Frage

I have an array containing objects of a custom-made class. However, on initialization the compiler gives me an error - "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];
                    };
}

Can someone tell me just where I am messing up and why I'm getting this error?

War es hilfreich?

Lösung

The Objective-C literal for an array is with square brackets,

NSArray *anArray = @[obj1, obj2].

In the code you posted it is trying to make a Dictionary,

NSDictionary *aDict = @{"key1" : obj1, @"key2" : obj2}

so this is why it is saying it expects a :.

The line should read,

self.rowArray = @[
    [[myClass alloc] initWithName:@"Harry" home:"New York" preference :Acura],
    [[myClass alloc] initWithName:@"Win" home:"Seattle" preference :Toyota];
];

As others have pointed out the there are a few other errors with the code and those city names are not NSString's, but I guess this is just an example snippet.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top