서로 다른 객체를 저장하는 클래스를 Collection이라고 한다.
- Array
- Dictionary
- Set
Define Type
NSRange
typedef struct _NSRange{
unsigned int location;
unsigned int length;
}RSRange;
초기화 : NSRange range = {17, 4};
NSRange range = NSMakeRange( 17, 4);
NSPoing
typedef strcut _NSPoint{
float x;
float y;
}NSPoint;
초기화 : NSMakePoing();
typedef struct _NSSize{
float width;
float height;
}NSSize;
초기화 : NSMakeSize();
typedef struct _NSRect{
NSPoint origin;
NSSize size;
}NSRect;
초기화 : NSMakeRect();
NSString
+ ( id ) stringWithFormat: (NSString *) format, ....;
- ( unsigned int )length;
- ( BOOL ) hasPrefix: (NSString *) string;
- ( BOOL ) hasSuffix: (NSString *) string;
- ( NSRange ) rangeOfString: (NSString *)string;
- ( NSArray ) componentSeperateByString: (NSString *)string;
예제 - 1)
NSString *str1 = @"Mon:Tue:Wen:Thu:Fri:Sat:Sun";
NSArray *days = [str1 componentSeperateByString:@":"];
for( int i = 0; i < [days count]; i++)
{
NSLog(@"Key : %@" , [days objectAtIndex:i]);
}
for( NSString *key in array)
{
NSLog(@"Key : %@", key);
}
NSArray *days = [str1 componentSeperateByString:@":"];
for( int i = 0; i < [days count]; i++)
{
NSLog(@"Key : %@" , [days objectAtIndex:i]);
}
for( NSString *key in array)
{
NSLog(@"Key : %@", key);
}
- ( BOOL ) isEqualToString: (NSString *)string;
- ( NSComparisonResult) compare: (NSString *) string;
- ( NSComparisonResult) compare: (NSString *) string option: (unsigned) mask;
NSComparisonResult의 enum
typedef enum _NSComparisonResult{
NSOrderedAscending = -1, NSOrderedSame, NSOrderedDescending
}NSComparisonResult;
mask
- NSCaseInsensitiveSearch : 대소문자를 무시하고 두 문자열을 비교한다.
- NSLiteralSearch : 대소문자를 구분하여 두 문자열을 비교한다.
- NSNumericSearch : 숫자문자를 숫자 그 자체로 인식
예제 ) Filename9.txt < Filename20.txt < Filename100.txt
- NSBackwardsSearch : 문자열의 뒷에서 앞으로 두 문자열을 비교한다.
- NSAnchoredSearch : 문자열의 시작과 끝으로 두 문자열이 같은지를 비교한다.
예제 - 1)
if( [string1 compare:string2 option: NSCaseInsensitiveSearch | NSnumericSearch ]
== NSOrderedSame)
{
NSLog(@"They match!");
}
== NSOrderedSame)
{
NSLog(@"They match!");
}
예제 - 2)
NSString *searchString = @"age";
NSString *beginsTest = @"Agencies";
NSRange prefixRange = [beginsTest rangeOfString:searchString
options:(NSAnchoredSearch | NSCaseInsensitiveSearch)];
// prefixRange = { 0, 3 }
NSString *endsTest = @"BRICOLAGE";
NSRange suffixRange = [endsTest rangeOfString:searching
options:(NSAnchoredSearch | NSCaseInsensitiveSearch | NSBackwarsSearch)];
// suffixRange = { 6, 3 }
NSString *beginsTest = @"Agencies";
NSRange prefixRange = [beginsTest rangeOfString:searchString
options:(NSAnchoredSearch | NSCaseInsensitiveSearch)];
// prefixRange = { 0, 3 }
NSString *endsTest = @"BRICOLAGE";
NSRange suffixRange = [endsTest rangeOfString:searching
options:(NSAnchoredSearch | NSCaseInsensitiveSearch | NSBackwarsSearch)];
// suffixRange = { 6, 3 }
예제 - 3)
NSString *string10 = @"string10";
NSString *string2 = @"string2";
NSComparisonResult result;
result = [string10 compare:string2];
// result = -1 ( NSOrderedAscending)
NSString *string2 = @"string2";
NSComparisonResult result;
result = [string10 compare:string2];
// result = -1 ( NSOrderedAscending)
result = [string10 compare:string2 options:NSNumbercSearch];
// result = 1 (NSOrderedDescending)
예제 - 4)
NSString *string_a = @"Aardvark";
NSString *string_A = @"AARDVARK";
NSString *string_A = @"AARDVARK";
result = [string_a compare:string_A];
// result = 0 ( NSOrderedDescending )
result = [string_a caseInsensitiveCompare:string_A];
// resutl = 0 (NSOrderedSame)
// equivalent to [string_a compare:string_A options:NSCaseInsensitiveCompareSearch]
예제 - 5) 파인더에서와 같은 스트링을 비교하고, 정렬하는 방법
int finderWithLocal(int string1, id string2, void *locale)
{
static int comparisonOptions =
NSCaseInsensitiveSearch | NSNumericSearch |
NSWidthInsensitiveSearch } NSForcedOrderingSearch;
{
static int comparisonOptions =
NSCaseInsensitiveSearch | NSNumericSearch |
NSWidthInsensitiveSearch } NSForcedOrderingSearch;
NSRange string1Range = NSMakeRange( 0, [string length]);
return [string1 compare:string2 option:comparisonOptions
range:string1Range
locale:(NSLocale *)locale];
}
NSArray *stringArray = [NSArray arrayWithObjects:
@"string 1",
@"string 21",
@"string 12",
@"string 11",
@"string 02", nil];
NSArray *sortedArray = {string Array sortedArrayUsingFunction:
finderSortWithLocale
context:[NSLocale currentLocale]];
// sortedArray contaings{"string1", "string02", "string11",
"string12", "string21"}
와 같은 결과를 가져온다.
NSMutableString : NSString이 Java의 String이라면 NSMutableString은 BufferedString이다
+ ( id ) stringWithCapacity: (unsigned)capacity;
- ( void ) appendString: (NSString*)string;
- ( void ) appendFormat: (NSString *) format, ...;
- ( void ) deleteCharactersInRange: (NSRange)range;
NSMutableString *string = [NSMutableString stringWithCapacity: 50];
NSArray : NSArray는 object만을 담는 구조체
NSArray *array = [NSArray arrayWithObjects: @"one", @"two", @"three", nil];
+ ( id ) arrayWithObjects:(id) object1,....;
- ( unsigned ) count;
- ( id ) objectAtIndex: (unsigned int) index;
- ( unsigned )indexOfObject: (id)object;
- ( NSString *)componedntJoinedByString: (NSString *)string;
NSMutableArray : Java에서 ArrayList 또는 Vector와 유사
+ ( id ) arrayWithCapacity: (unsigned)numItems;
- ( void ) addObject: (id) object;함
- ( void ) removeObjectAtIndex: (unsigned)index;
- ( void ) removeObject: (id)object;
- ( void ) removeAllObjects;
- ( void ) insertObject: ( id )object atIndex:(unsigned)index;
예제 - 1)
NSMutableArray *array = [NSMutableArray arrayWithCapacity: 10]
[array addObject:@"AAAA"];
[array addObject:@"BBB"];
[array addObject:@"CCCCC"];
[array addObject:@"AAAA"];
[array addObject:@"BBB"];
[array addObject:@"CCCCC"];
NSEnumerator *elem = [array objectEnumerator];
id item;
while( item = [elem nextObject]){
NSLog(@"%@", item);
}
for( NSString *elem in array){
NSLog(@"%@", elem);
}
NSDictionary : Java에서 말하는 Hashtable와 유사함
+ ( id ) dictionaryWithObjectsAndKeys: (id) firstObject, ...;
- ( unsigned ) count;
- ( id ) objectForKey: (id) key;
예제 - 1)
Student *s1 = [Student new];
Student *s2 = [Student new];
Student *s3 = [Student new];
Student *s2 = [Student new];
Student *s3 = [Student new];
NSDictionary * students = [NSDictionary dictionaryWithObjectsAndKeys:
s1, @"101", s2, @"102", s3, @"103", nil];
Student *s = [students objectForKey: @"102"];
NSMutableDictionary : 크기가 변경하는 NSDictionary
+ ( id ) dictionaryWithCapacity: (unsigned int) numItems;
- ( void ) setObject: (id) object forKey: (id) key;
- ( void ) removeObjectForKey: (id) key;
- ( void ) removeAllObjects;
NSSet : 순서가 없는 객체의 컬렉션을 위한 인터페이스
+ setWithObjects: ( id )firstObj, ....//마지막에 nil로 끝낼 것
- ( unsigned ) count;
- ( BOOL ) containsObject: (id) object;
NSMutableSet : 순서가 없이 추가 삭제할수 있음
+ ( NSMutableSet *) set;
- ( void ) addObjet: (id)object;
- ( void ) removeObject: (id) object;
- ( void ) removeAllObject;
- ( void ) intersectionSet: (NSSet *)otherSet;
- ( void ) minusSet: (NSSet *)otherSet;
NSNumber : NSArray, NSMutableArray, NSDictionary, NSMutableDictionary는
오직 Object만을 담을수 있다. NSNumber는 object로 변환하여 해당 구조에
데이터를 담을수 있도록 해준다.
+ ( NSNumber *) numberWithChar: (char)value;
+ ( NSNumber *) numberWithInt: (int)value;
+ ( NSNumber *) numberWithFloat: (float)value;
+ ( NSNumber *) numberWithBool: (BOOL) value;
- (char) charValue;
- (int) intValue;
- (float) floatValue;
- (NSString *) stringValue;
Enumeration : 컬렉션에 포함된 데이터에 쉽게 접근하기 위한 방법, NSArray, NSDictionary, NSSet와 같이 모든 컬렉션에 사용가능
예제 - 1)
NSArray *array = ...;
for ( People *people in array ){
NSLog([people description]);
}
'개발 > Objective-C' 카테고리의 다른 글
[Objective-C] tokenizerWithString in ParseKit (0) | 2010.08.29 |
---|---|
[Objectiv-C] UILabel과 관련된 주요 설정값 (0) | 2010.08.29 |
[Objective-C] Memory 관리 (0) | 2010.08.29 |
SEl, @selector (0) | 2010.08.29 |
클래스, 셀렉터, 프로토콜 대충 요약 (0) | 2010.08.29 |