开发者

Objective-C - simple setting property example erroring with 'request for ___ in something not a structure or union'

I've been banging my head with this one this evening and am sure it's something very simple that I've missed

I've created a new project with the appdelegate and a view controller class as below. The view controller synthesises the property in the .m, and the app delegate header imports the view controller .h file. Code below:

View controller header:

@interface untitled : UIViewController {
    NSString *string;
}
@property (nonatomic, retain) NSString *string;

App delegate:

- (void)applicationDidFinishLaunching:(UIApplication *)application {
 testViewController = [[un开发者_JAVA百科titled alloc] initWithNibName:@"untitled" bundle:nil];
 testViewController.string = @"Testing String";
    [window addSubview:testViewController.view];
    [window makeKeyAndVisible];
}

Can someone please help and point out the obvious mistake as to why setting the string property fails here with the error mentioned? Is it because of being inside this method? I've never had issues setting properties in other methods before after initing a view controller.

Thanks.


The error is saying it does not understand that the class has that property. It means you have either the wrong class or that it knows nothing about the class.

So, you need to add:

#import "untitled.h" 

in your application delegate - also, you need to have the variable be of type "untitled" (I am pretty sure you declared the type as UIViewController and not untitled):

 untitled * testViewController = (untitled *)[[untitled alloc] initWithNibName:@"untitled" bundle:nil];

By the way, by convention you should always start class names in uppercase.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜