Hatena::Groupfragments

うまい棒の断片

2010-08-26

[]Cocoa Foundation memo 01:17 はてなブックマーク - Cocoa Foundation memo - うまい棒の断片

  • メソッド呼び出し

[Class(or instance) methodo: arg1]

変態記法

  • NSRange

文字列の位置と長さ

  • NSPoint

座標

  • NSString

イミュータブルな文字列。@"hoge" 。

  • (id) stringWithFormat:

printf的な。"%@" はobjective-cオブジェクト

NSString *foo;
foo = [NSString stringWithFormat: @"foo bar baz %d", 100];
  • (BOOL) isEqualToString:

文字列の比較

  • (NSComparisonResult) compare:

文字列の比較。戻り値がbooleanじゃなくてオブジェクト。(左が大きかったら1とか)

    • option

ignorecase したいときは

if ( [foo compare: bar options: NSCaseInsensitiveSearch]
== NSOrderdSame) {
NSLog(@"hogehoge");
}
  • (BOOL) hasPrefix:
  • (BOOL) hasSuffix:
  • NSMutableString

ミュータタブルな文字列

appendしたりdeleteしたり。

  • (id) stringWithCapacity

文字列の箱作成メソッド。引数の数字はよくわからん。

ヒントらしいけど、最大値とかいうわけでもないし。

NSMutableString *foo;
foo = [NSMutableString stringWithCapacity:100];
  • (void) appendString
  • (void) appendFormat
  • (void) deleteCharactersInRange: (NSRange) range;
  • NSArray

イミュータブルな配列。格納できるのはobjective-cオブジェクトのみ。

int、floatとかは無理。(やってみたらセグフォした)

最後の要素にはnilを。

NSArray *array;
array = [NSArray arrayWithObjects: @"foo", @"bar", @"baz", nil];
  • (unsinged) count

数を返すメソッド。

  • (id) objectAtIndex

普通の配列でいう foo[10] みたいなことをやる

int i;
for (i = 0; i < [array count]; i++) {
NSLog(@"array[%d] => %@", i, [array objectAtIndex: i];
}
  • NSMutableArray

ミュータブルな配列。追加/削除可能

  • (id) arrayWithCapacity

stringWithCapacityと同様に引数の数値が謎。

めんどくさ

  • (NSEnumerator *) objectEnumerator;
  • (id) nextObject
NSEnumerator *enumerator;
enumerator = [array objectEnumerator];
id foo;
while (foo = [enumerator nextObject]) {
NSLog("hogehoge %@", foo);
}
  • objective-2.0 以降 (leopard以降)

LLっぽいforで。

for (NSString *foo in array) {
NSLog(@"hoge %@", foo);
}
  • NSDictionary

連想配列、いわゆるハッシュ。値もキーもobjective-cオブジェクト

  • dictionaryWithObjectsAndKeys

長いわ。値、キーっていう順番がしっくりこない。

  • (id) objecForKey:
NSDictionary *foo;
foo = [NSDictionary dictionaryWithObjectsAndKeys:
[MyClass new], @"hoge"]
MyClass *bar = [foo objectForKey: @"hoge"];