我有一个包含自定义类对象的数组。但是,在初始化时,编译器会给我一个错误 - "Lexical or Preprocessor" Expected ':'
interface myClass : NSObject
@property (readwrite, strong) NSString* name;
@property (readwrite, strong) NSString* home;
@property (readwrite, strong) Preference pref; // This is another custom class
-(id) initWithName:(NSString*) name home:(NSString*) home preference:(Preference) preference;
end
@interface MyViewController()
@property (nonatomic, strong) NSArray *rowArray;
@end
@implementation MyViewController
...
...
...
- (void) initializeArray
{
self.rowArray = @{
[[myClass alloc] initWithName:@"Harry" home:@"New York" preference :Acura],
[[myClass alloc] initWithName:@"Win" home:@"Seattle" preference :Toyota];
};
}
有人可以告诉我我弄乱的地方以及我为什么会收到此错误?
答案 0 :(得分:8)
数组的Objective-C literal带方括号
NSArray *anArray = @[obj1, obj2]
。
在您发布的代码中,它正在尝试创建一个词典,
NSDictionary *aDict = @{"key1" : obj1, @"key2" : obj2}
所以这就是它要求:
。
该行应该是
self.rowArray = @[
[[myClass alloc] initWithName:@"Harry" home:"New York" preference :Acura],
[[myClass alloc] initWithName:@"Win" home:"Seattle" preference :Toyota];
];
正如其他人指出的那样,代码中还有一些其他错误,而这些城市名称不是NSString,但我想这只是一个示例代码段。