11
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Objective-Cの文法メモ

Last updated at Posted at 2012-07-10

###文字列操作

// 結合
NSMutableString * string = [NSMutableString stringWithString:@"aaa"];
NSString *joined = [string appendString:@"bbb"];

// 配列を結合
NSArray *array = [NSArray arrayWithObjects:@"a", @"b", @"c", nil];
NSString *joined = [array componentsJoinedByString:@","];

//配列に分割
NSString * string = [NSMutableString stringWithString:@"aaa,bbb,ccc"];
NSArray *record = [string componentsSeparatedByString:@","];

// 長さ
int length = [@"abcdef" length];

//切り出し
NSString *string = [@"abcd" substringWithRange:NSMakeRange(0, 2)];

//検索
NSString *string = @"abcd";	
NSRange range = [string rangeOfString:@"cd"];
NSLog(@"%d:%d", range.location, range.length);

###配列

// 作成
NSArray* array = [NSArray arrayWithObject:string];
NSArray* array = [NSArray arrayWithObjects:@"A", @"B", @"C", nil];

// 取得
NSString* thirdObj = [array objectAtIndex:2];
for (NSString* string in array) {
    NSLog(string);
}

// 追加
NSMutableArray* array = [NSMutableArray array];
[array addObject:@"A"]; 
NSArray* arrayB = [NSArray arrayWithObjects:@"B", @"C", @"D", nil];
[array addObjectsFromArray:arrayB];

// 挿入
[array insertObjects:@"E" atIndex:0];

// 置換
[array replaceObjectAtIndex:1 withObject:@"F"];

###辞書

// 作成
NSDictionary* dic = [NSDictionaryWithObject:@"Taro" forKey:@"name"];

// 追加
NSDictionary * dic = [NSDictionary dictionary];
[dic setObject:obj ForKey:@"key"];

// 取得
NSString* str = [dic objectForKey:@"key"];
for (id key in dic) {
    NSLog(@"key: %@, value: %@\n", key, [dic objectForKey:key]);
}

###例外処理

@try {
    // something
}
@catch (NSException *e) {
    NSLog(@"Error: %@: %@", [e name], [e reason]);
}
@finally {
    // something
}
NSException *exception = [NSException exceptionWithName:@"hoge" reason:@"foo" userInfo:nil];
@throw exception;
11
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?