Objective-C æéåºç¤ææ³ãã¹ã¿ã¼
Java基礎文法最速マスター - いろいろ解析日記ãåèã«ãObjective-Cã®ãã®ãæ¸ãã¦ã¿ãã
ã¾ã ã¾ã æ´ãæµ ãã®ã§ééã£ã¦ããæãããããããã¾ããã
[2012-08-03追è¨]
ARCããªãã©ã«è¡¨è¨ãªã©æ°ããè¨æ³ãå°å
¥ããããã®ãã¼ã¸ã«è¨è¼ããã¦ãããã¨ã¯å¤å°å¤ããªãã¤ã¤ããã¾ãããã ãã¨ã£ãããã¨ãã¦ãããèªãã§ã¿ãç¨åº¦ã§è¯ãã¨æãã¾ããä»ãè²ããªäººã解説è¨äºãåºãã¦ããã®ã§ããã¡ããæ¯éåèã«ãªãã£ã¦ãã ããã
追è¨ããã
åºç¤
ãããã¬ã³ã³ã½ã¼ã«åºå
Xcodeã®ãããã¬åºåãè¡ãã«ã¯NSLogé¢æ°ã使ãã¾ãã
printfã¨åæ§ã®ãã©ã¼ãããæååã使ãã¾ããã%@ã¯NSStringã®ã¤ã³ã¹ã¿ã³ã¹ã表示ããæã«ä½¿ç¨ãã¾ãã
NSLog(@"message: %@", messge); NSLog(@"object: %@", [object description]);
â» ã%@ãã¯descriptionã¡ã½ãããå¼ã³åºãã¦æåååãã¦ããã®ã§ã[object description]ã¨ãã風ã«ããããæ¸ãã®ã¯ç¡é§ã§ãããã³ã¡ã³ããææãããã¨ããããã¾ããã(2011/10/07)
ã³ã¡ã³ã
ã³ã¡ã³ãã§ãã
// ä¸è¡ã³ã¡ã³ã /* è¤æ°è¡ã³ã¡ã³ã */
å¤æ°å®£è¨
å¤æ°ã®å®£è¨ã§ããCã¨åãã§ããå¤æ°ã®å®£è¨æã«ã¯ãã¼ã¿åãæå®ãã¾ãã
int n;
ãã¼ã¿åã¯Cã®ãã¼ã¿åã使ãã¾ãã
int n; unsigned int n; char n; unsigned char n; long n; float n; double n;
çå½å¤ã¯YES,NOãç¨æããã¦ãã¾ãã
BOOL isOK = YES; BOOL isBAD = NO;
Objective-Cã®ãªãã¸ã§ã¯ãã®å®£è¨ã¯ãã¤ã³ã¿ã®å®£è¨ã§ãã
NSString *string; NSArray * array; NSDictionary* dictinary;
ãªãã¸ã§ã¯ãã®çæã¨ã¡ã¢ãªè§£æ¾
ãªãã¸ã§ã¯ãçæã«ã¯ãalloc+initç³»ã¨autoreleaseæ¸ã¿ã®2éããããã¾ãã
GCãªãã®å ´åã¡ã¢ãªç®¡çã¯åç
§ã«ã¦ã³ãæ¹å¼ã¨ãªãã®ã§ã使ç¨ã«ã¯æ³¨æãå¿
è¦ã§ãã
åç
§ã«ã¦ã³ããã¤ã³ã¯ãªã¡ã³ãããã«ã¯retain,ãã¯ãªã¡ã³ãããã«ã¯releaseã使ãã¾ãã
alloc+initç³»
åæåæã«alloc+initç³»ã¡ã½ããã使ã£ãå ´åã«ã¯ åç §ã«ã¦ã³ãã1ã®ç¶æ ãªã®ã§ã使ç¨ãçµããã°releaseãã¦è§£æ¾ããå¿ è¦ãããã¾ãã
NSArray *array = [[NSArray alloc] initWithObjects:@"a", @"b", @"c", nil]; // ãªããå¦ç [array release]; // 解æ¾
alloc+initç³»ã¡ã½ããã使ã£ã¦çæãããªãã¸ã§ã¯ããautoreleaseã«ä»»ãããã¨ãã§ãã¾ãã
NSArray *array = [[[NSArray alloc] initWithObjects:@"a", @"b", @"c", nil] autorelease];
autoreleaseæ¸ã¿
autoreleaseã§åæåãããªãã¸ã§ã¯ããåç §ã«ã¦ã³ãã1ã§ãããautoreleaseã®ã¹ã³ã¼ããåããæç¹ã§releaseã¡ãã»ã¼ã¸ãéä¿¡ãããã¡ã¢ãªãèªåçã«è§£æ¾ããã¾ãã
NSArray *array = [NSArray arrayWithObjects:@"A", @"B", @"C", nil];
æ°å¤
int i = 2 int i = 100000000; float radius = 1.0f; double diameter = 2 * M_PI * radius;
ååæ¼ç®
num = 1 + 1; num = 1 - 1; num = 1 * 2; num = 1 / 2;
åã®æ±ãæ¹ã§ããå²ãæ°ã¨å²ãããæ°ã両æ¹ã¨ãæ´æ°ã®å ´åãè¨ç®çµæã®å°æ°ç¹ä»¥ä¸ãåãæ¨ã¦ããã¾ãã
num = 1 / 2; // 0
å²ãæ°ã¨å²ãããæ°ã®ã©ã¡ãããå°æ°ã®å ´åãè¨ç®çµæã®å°æ°ç¹ä»¥ä¸ãåãæ¨ã¦ããã¾ããã
num = 1.0 / 2; // 0.5 num = 1 / 2.0; // 0.5 num = 1.0 / 2.0; // 0.5
ä½ãã®æ±ãæ¹ã§ãã
// ä½ã mod = 4 % 2ï¼
ã¤ã³ã¯ãªã¡ã³ãã¨ãã¯ãªã¡ã³ã
// ã¤ã³ã¯ãªã¡ã³ã num++; ++num; // ãã¯ãªã¡ã³ã num--; --num;
åä½ã§ä½¿ç¨ããå ´åã«ã¯ã©ã¡ãã使ç¨ãã¦ãåé¡ãªãã§ãããä»£å ¥å¼ãæ¡ä»¶å¼ã§è©ä¾¡ããå ´åã«ã¯æ³¨æãå¿ è¦ã§ãã
æåå
æååã®è¡¨ç¾
æååã¯NSStringã使ãã¾ãããªãã©ã«è¡¨ç¾ã¨ãã¦@""ã使ãã¾ãã
NSStringã¯å¤æ´ä¸å¯è½ãªæååã§ãã
NSString *string = @"Hello World!";
å¤æ´å¯è½ãªæååã¯
NSMutableString * string = [NSMutableString stringWithString:@"Hello World!"];
æååæä½
// çµå 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)]; // ab //æ¤ç´¢ NSString *string = @"abcd"; NSRange range = [string rangeOfString:@"cd"]; NSLog(@"%d:%d", range.location, range.length); //ã¿ã¤ãããªãå ´åã«ã¯length = 0ã«ãªã
é å
宣è¨
NSArray *array;
é åã®çæ
NSArray *array; array = [NSArray arrayWithObjects:@"a", @"b", @"c", nil]; NSMutableArray *array = [NSMutableArray array]; // å¤æ´å¯è½ãªé åã宣è¨ã¨åæã«ä»£å ¥
è¦ç´ ã®åç §ã¨ä»£å ¥
// è¦ç´ ã®åç § [array objectAtIndex:0]; [array objectAtIndex:1]; // ä»£å ¥(NSMutableArrayã®ã¤ã³ã¹ã¿ã³ã¹) [array removeObjectAtIndex:1]; // ä¸åº¦è¦ç´ ãåé¤ã㦠[array insertObject:@"1" atIndex:1]; // åé¤ããã¨ãã«æ¿å ¥
é åã®è¦ç´ æ°
int array_num = [array count];
è¾æ¸
宣è¨
NSDictionary *dictinary; dictionary = [NSDictionary dictionaryWithObjectsAndKeys: @"value1",@"key1", @"value2",@"key2", nil, ]; NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; // å¤æ´å¯è½ãªè¾æ¸ã宣è¨ã¨åæã«ä»£å ¥
è¦ç´ ã®åç §ã¨ä»£å ¥
//è¦ç´ ã®åç § id val1 = [dictionary objectForKey:@"key1"]; id val2 = [dictionary objectForKey:@"key2"]; // ä»£å ¥(NSMutableDictionaryã®ã¤ã³ã¹ã¿ã³ã¹) [dictionary setObject:@"value1" forKey:@"key1"];
è¾æ¸ã®æä½
ã»ãã¼ã®åå¾
NSArray *keys = [dictionary allkeys]
ã»å¤ã®åå¾
NSArray *values = [dictionary allValues];
ã»ãã¼ã®åé¤(NSMutableDictionaryã®ã¤ã³ã¹ã¿ã³ã¹)
>|objc||
[dictionary removeObjectForKey:@"key1"];
|
å¶å¾¡æ
ifæ
if ( æ¡ä»¶ ) {
}
if ã elseæ
if (æ¡ä»¶) { } else { }
if ã else if æ
if ( æ¡ä»¶ ) { } else if ( æ¡ä»¶ ) { }
whileæ
int i = 0; while (i < 5) { // ãªããå¦ç i++; }
foræ
for (int i = 0; i < 5; i++) { //å¦ç }
é¢æ°å®ç¾©
åºæ¬çã«ã¯ã©ã¹ã®ã¡ã½ããã¨ãã¦å®ç¾©ãã¾ãããCè¨èªã®é¢æ°å®ç¾©ãå¯è½ã§ãã
ã¾ããæ¢åã®ã¯ã©ã¹ãæ¡å¼µããæ¹æ³ã¨ãã¦ã«ãã´ãªã¨ãããã®ãããã¾ãã
@interface ClassA : NSObject { NSString *name_; } @property (nonatomic, copy) NSString *name_; @implementation ClassA @synthesize name_ - (void) helloWorld:(NSString *)name { NSLog(@"hello %@ world! from %@", name, self.name_); } @end
ãã¡ã¤ã«å ¥åºå
NSString *inputFilePath = [INPUTFILEPATH stringByExpandingTildeInPath]; NSString *outputFilePath = [OUTPUTFILEPATH stringByExpandingTildeInPath]; NSFileManager *fm = [NSFileManager defaultManager]; if ( ![fm fileExistsAtPath:outputFilePath] ) { [fm createFileAtPath:outputFilePath contents:nil attributes:nil]; } NSFileHandle *input = [NSFileHandle fileHandleForReadingAtPath:inputFilePath]; NSFileHandle *output = [NSFileHandle fileHandleForWritingAtPath:outputFilePath]; @try { NSData *data; while( (data = [input readDataOfLength:1024]) && 0 < [data length] ){ [output writeData: data]; } } @finally { [input closeFile]; [output closeFile]; }
ç¥ã£ã¦ãããã»ããããææ³
é«éåæ
NSArrayãNSDictionaryã¯é«éåæã¨ããæ¹æ³ã«ãã£ã¦è¦ç´ ã®åæãç°¡åã«ã§ãã¾ã
NSArray *array = [NSArray arrayWithObjects:@"A", @"B", @"C", nil]; for (id i in array) { //ãªããå¦ç }
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys: @"value1", @"key1", @"value2", @"key2", @"value3", @"key3", nil ]; // ãã¼ã§ã«ã¼ãããå ´å for (id i in [dictionary keyEnumerator]) { // ãªããå¦ç } // å¤ã§ã«ã¼ãããå ´å for (id i in [dictionary objectEnumerator]) { // ãªããå¦ç }
ä¸æå¤æ°ã¯idã§åãã¦ããããããã¼ã®åãå¤æãã¦ããæã«ã¯åãæå®ããæ¹ãè¯ãã§ãããã
for (NSString *k in [dictionary keyEnumerator]) { // å¦ç }
ã«ãã´ãª
LLã®ããã«æ¢åã®ã¯ã©ã¹ã«ã¡ã½ããã追å ãããã¨ãã§ãã¾ãã
@interface NSString (Decorate) // ã«ãã´ãªã«å±ããã¡ã½ããå®ç¾©ãè¡ã - (NSString *) decorateWithString:(NSString *)string; @end @implementation NSString (Decorate) - (NSString *) decorateWithString:(NSString *)string { return [NSString stringWithFormat:@"%@%@%@", string, self, string]; } @end
NSLog(@"test: %@",[@"[MSG]" decorateWithString:@"**"]); // **[MSG]**
ãããã³ã«
ã¯ã©ã¹ããããã³ã«ã«é©åãã¾ãã¨å®£è¨ãããå ´åããããã³ã«ã§è¦å®ããã¦ããã¡ã½ãããå®è£ ããªããã°ãªããªãããã«ãããã¨ãã§ãã¾ãã
ããããã£
ã¯ã©ã¹ã®ã¡ã³ãå¤æ°ã¸ã®ã¢ã¯ã»ãµãæä¾ããæ©è½ã§ãã
使ç¨ããã«ããã£ã¦ç¥ã£ã¦ããã¹ããã¨ãããã¤ãããã¾ãããã²ã¨ã¤ã ãç´¹ä»ãã¾ãã
@property (nonatomic, retain) NSArray *array_;
ä¸è¨ã®ããã«retainã§å®£è¨ãããããããã£ã¸ã®ä»£å ¥ã¯ã¢ã¯ã»ãµãéããªãã¨retainãæå¹ã«ãªãã¾ããã
- (id)initWithParam:(NSArray *)array { if (self = [super init]) { array_ = array; // retainãããªãã self.array_ = array; // retainãããã } return self; } - (void)dealloc { [array_ release]; [super dealloc]; }
retainããã¦ããªãç¶æ ã¨ãªãã®ã§ãèªåãä¿æãã¦ããã«ããããããå¤é¨ã®å¦çã§releaseããããã¨ã§ãªãã¸ã§ã¯ããç ´æ£ãããå¯è½æ§ãããã¾ããç ´æ£ããã¦ãã¾ãã¨EXC_BAD_ACCESSãããã¯double freeãçºçãã¾ãã
åå¿è ã«ã¯æ°ã¥ãã«ããã¨æããã¾ãã®ã§ã注æãã ããã
- æ¤è¨¼ãã¦ã¿ãï¼アクセサ通さないとretainされない件 - fn7の日記
- ã¢ã¯ã»ãµãå¼·å¶ããã«ã¯ï¼Googleスタイルガイドのプロパティ定義でアクセサを強制 - fn7の日記
Google Objective-Cã¹ã¿ã¤ã«ã¬ã¤ã æ¥æ¬èªè¨³
ã¾ã å°ãããèªãã§ãªããã©ããããåå¼·ã«ãªãã¾ãã
ãã£ã¨è©³ããç¥ãããå ´å
ãã¡ããåèã«ãã¦ãã ããã
[åèè³æ]
- Cocoaでファイル入出力 -- BONNOH FRACTION 14
- NSRangeを作るのなら NSMakeRange関数を使う - iRSSの日記 - iPhoneアプリ開発グループ
- XcodeのgdbでObjective-Cのメソッドを呼び出す - Hello, world! - s21g
- 株式会社アンタス - 次の時代のマーケティングテクノロジーを
- http://lifeaether.homeunix.net/overtaker/blog/?p=1180
- Objective-Cのアクセサの話 : As Sloth As Possible(20100603追å )
- http://lifeaether.homeunix.net/overtaker/blog/?page_id=1403(20100621追å )
ã³ãã³ãã©ã¤ã³ã§ã³ã³ãã¤ã«
æå¤ã¨ç°¡åã«ã³ã³ãã¤ã«ãã§ããããã ã
Xcodeããé©å½ãªããã¸ã§ã¯ããä½æãã¦main.mã ãã¨ãåºãã
main.mãé©å½ã«ç·¨éãã¡ããã
#import <Cocoa/Cocoa.h> int main(){ NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSLog(@"Hello World!"); [pool release]; return 0; }
ã³ã³ãã¤ã«ãã¦ã¨ã©ã¼ããªããã°å®è¡ããã
$ gcc main.m -o main -framework Cocoa && ./main
[åè]
あきおの日記