NSString s1; //compile error: interface type cannot be statically allocated
NSString *s2 //good
é™æ€æ„味ç€è¯¥å¯¹è±¡çš„内å˜æ˜¯åœ¨ç¼–译时分é…çš„å—?但是在è¿è¡Œæ—¶åˆ†é…æ‰€æœ‰å¯¹è±¡å¹¶é€šè¿‡æŒ‡é’ˆè®¿é—®çš„åŽŸå› æ˜¯ä»€ä¹ˆï¼Ÿ 我知é“指针å…许更好地使用内å˜ï¼Œä¾‹å¦‚将它们作为方法的å‚æ•°ä¼ é€’ï¼Œä½†ä¸ºä»€ä¹ˆç¦æ¢é™æ€åˆ†é…?我是这ç§è¯è¨€çš„新手,我想了解它的愿景。
ç”案 0 :(得分:5)
在C ++ä¸ï¼Œé™¤éžæœ‰æŒ‡å‘该对象的指针,å¦åˆ™ä¸èƒ½æ‹¥æœ‰å¤šæ€å¯¹è±¡ã€‚ Objective-Cä¸å…è®¸ä½ æ‰“å¼€é‚£äº›è •è™«ã€‚å®ƒæ˜¯ä¸€ç§åŠ¨æ€ç±»åž‹è¯è¨€ï¼ˆå¦‚æžœä½ æ„¿æ„,å¯ä»¥æ供很多ä¿æŠ¤ï¼‰ - 在编译时é”å®šä½ çš„ç±»åž‹è¿èƒŒäº†è¯è¨€çš„哲å¦ã€‚
NSString *s2 = [[NSString alloc] initWith ???];
考虑那æ¡çº¿ã€‚ä½ æ£åœ¨åˆ†é…一个å—符串对象(我故æ„å°†æž„é€ å‡½æ•°çš„ç»†èŠ‚ç•™ç©ºï¼‰ã€‚æ‚¨å°†æ— æ³•èŽ·å¾—NSString的实际实例 - 您的对象类型将是ç§æœ‰å类。这个概念å«åšClass Clusters。
å–决于è¿è¡Œçš„æž„é€ å‡½æ•°ä»¥åŠä¼ 入的å‚æ•°å¯èƒ½ä¼šå½±å“è¿è¡Œæ—¶ç±»åž‹ã€‚对于é™æ€åˆ†é…的对象,这是ä¸å¯èƒ½çš„。
å½“ç„¶ï¼Œä½ çš„é—®é¢˜è¿˜æœ‰å¾ˆå¤šï¼Œä½†è€ƒè™‘åˆ°æ¯ä¸ªå¯¹è±¡åœ¨è¿è¡Œæ—¶éƒ½æ˜¯åŠ¨æ€çš„,所有这些都是å¯èƒ½çš„。还è¦è€ƒè™‘è¿™é¿å…了C ++çš„å¤æ‚性。
ç”案 1 :(得分:1)
我对Objective-C有所了解。 (指针世界)。
// In Objective C, you work with pointers. And In fact you cast them in order to help the compiler help you.
NSString *s1; // This means: hey compiler I expect s1 points to a NSString.
id s2; // id is the same like (anyKind *), It´s a pointer to an object, no matter how what kind.
// s1 and s2 are in fact the same;
// Try this:
NSArray *array = @[@"Understanding",@"Objective C"];
s2 = array;
s1 = s2;
NSLog(@"Let´s go to see s2 store object: %@",[s2 description]);
NSLog(@"Let´s go to see s1 store object: %@",[s1 description]);
// All work, because the type you declare is in order to help the compiler, this doesn´t have any effect a runtime.
// In fact, object has problem a runtime, If they receive a selector (methods) what they don´t know. In this case
// the selector sended is knowing for all kind of objects.
// If you change the order and assign first s1, the compiler could help you, and a warning there will be.
// Tomorrow more.