Objective-C 클래스의 열거
Objective-C 클래스의 열거 (for-in)
Objective-C의 고속 열거(Fast enumeration)는 Cocoa 컨테이너 클래스의 요소들을 일반적인 방식으로 열거하게 해주는 구조예요. Objective-C에서는 for-in 루프로 구현돼요.
출처: Free Pascal Reference - Enumeration in Objective-C classes
본문
이 기능은 기존의 for-in 루프 메커니즘을 활용해 Objective-Pascal로 옮겨졌어요. 그래서 두 언어에서 동일하게 동작해요. 단, objectivec2 모드 스위치가 활성화되어 있어야 한다는 점을 기억하세요.
다음은 for-in 사용 예시예요.
{$mode delphi}
{$modeswitch objectivec2}
uses
CocoaAll;
var
arr: NSMutableArray;
element: NSString;
pool: NSAutoreleasePool;
i: longint;
begin
pool:=NSAutoreleasePool.alloc.init;
arr:=NSMutableArray.arrayWithObjects(
NSSTR('One'),
NSSTR('Two'),
NSSTR('Three'),
NSSTR('Four'),
NSSTR('Five'),
NSSTR('Six'),
NSSTR('Seven'),
nil);
i:=0;
for element in arr do
begin
inc(i);
if i=2 then
continue;
if i=5 then
break;
if i in [2,5..10] then
halt(1);
NSLog(NSSTR('element: %@'),element);
end;
pool.release;
end.
더 알아보기
for-in루프 자체의 문법과 규칙은 배열·컬렉션 반복 관련 내용에서 다뤄요.objectivec2모드 스위치는 Objective-C 2.0 기능을 켜는 데 필요해요.