I am converting an objective-C OSX project which is manual memory managed, to ARC.
For that I selected the project -> Edit -> Convert -> To Objective-C ARC.
The I get several errrors and I am solving them one by one. Below is an error shown as Semantic issue. that I do not understand.
Semantic Issue, AppController.m:263:21: Instance method 'window' not found ; did you mean 'windows'?
Warning: AppController.m:263:21: Instance method '-window' not found (return type defaults to 'id'); did you mean '-windows'?
The code is below
[[[NSApp delegate] window] setTitle:winTitle];
The AppDelegate does have a window property, I am not sure, why is xcode complaining about it.
#import <Cocoa/Cocoa.h>
#import "AppController.h"
#import "UpdateWindowController.h"
#interface AppDelegate : NSObject <NSApplicationDelegate>
{
NSWindow* _window;
AppController* _appController;
UpdateWindowController* updateWinController;
}
#property (assign) IBOutlet NSWindow *window;
#property (assign) IBOutlet AppController *appController;
#end
Related
I am getting new warning on my old OSX app. I am using OSX 10.10 and I am not quite sure where the problem is. Can someone help?
The actual warning is Property type 'id is incompatible with type id inherited from NSTextField
#import <Cocoa/Cocoa.h>
#import "HyperlinkTextFieldDelegate.h"
#interface HyperlinkTextField : NSTextField <NSTextFieldDelegate>
#property (assign) id <HyperlinkTextFieldDelegate> delegate; <--- warning showing up here
#end
The main implementation is
#interface HyperlinkTextField ()
#property (nonatomic, readonly) NSArray *hyperlinkInfos;
#property (nonatomic, readonly) NSTextView *textView;
- (void)_resetHyperlinkCursorRects;
#end
#define kHyperlinkInfoCharacterRangeKey #"range"
#define kHyperlinkInfoURLKey #"url"
#define kHyperlinkInfoRectKey #"rect"
#implementation HyperlinkTextField
#synthesize delegate;
And the delegate file is
#import <Foundation/Foundation.h>
#protocol HyperlinkTextFieldDelegate <NSObject>
- (void) barLinkClicked: (id) sender;
#end
NSTextField already has a delegate property, and it is typed as id<NSTextFieldDelegate>. Thus, your HyperinkTextField, which is a subclass of NSTextField, inherits this property, just as the error message clearly tells you. You cannot override this inherited property and type it as id<HyperlinkTextFieldDelegate> where that is a different type.
Hi i've recently completed a tutorial book on the basics of objective c. And now I'm "Attempting" to make a simple application. Right now I seem to be having the simplest issues which i cannot seem to fix, maybe you could tell me what i'm doing incorrectly.
AppDelegate.h
#import <Cocoa/Cocoa.h>
#interface AppDelegate : NSObject <NSApplicationDelegate>
#property (assign) IBOutlet NSWindow *window;
- (IBAction)saveData:(id)sender;
//Below is a simple IBOutlet which I try to retrieve data from when the IBAction saveData occurs
#property (weak) IBOutlet NSTextField *foodName;
#end
AppDelegate.m
#import "AppDelegate.h"
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
}
//Here is the saveData IBAction which should print out the value of the NSTextField if the user entered a value (if not returns null)
- (IBAction)saveData:(id)sender {
NSLog(#"%#", foodName.stringValue);
NSLog(#"Saved");
}
#end
The problem I seem to have is the build will fail and give me an error message on this line in AppDelegate.m:
NSLog(#"%#", foodName.stringValue);
the error message is: Use of undeclared identifier 'foodName'; did you mean '_foodName'?
Could someone please explain whats going on and how I can overcome this?
To address the getter method, use self.:
NSLog(#"%#", self.foodName.stringValue);
Also, a minor point: group all your #property declarations together at the start of the #interface statement, but after any instance variable declarations:
#interface MyClass : NSObject {
NSString *_str1;
int _i1;
}
#property (weak) IBOutlet NSString *something;
#property (strong, readonly) NSNumber *somethingElse;
- (int)aMethod:(NSString *)string;
- (void)anotherMethod;
#end
This question already has an answer here:
Existing ivar 'title' for unsafe_unretained property 'title' must be __unsafe_unretained
(1 answer)
Closed 9 years ago.
Same code are already questionned here, but I deal with a different problem that I can't solve myself probably because i'm new with Objective-C, so I decide to ask the question :)
webberAppDelegate.h:
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#interface webberAppDelegate : NSObject <NSApplicationDelegate> {
NSWindow *window;
WebView *webber;
}
#property (assign) IBOutlet NSWindow *window;
#property (assign) IBOutlet WebView *webber;
#end
webberAppDelegate.m:
#import "webberAppDelegate.h"
#implementation webberAppDelegate
#synthesize window;
#synthesize webber;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSString *urlString = #"http://www.apple.com";
// Insert code here to initialize your application
[[webber mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
}
#end
So, in webberAppDelegate.m, here's my problem with this fraction I suppose:
#synthesize window;
#synthesize webber;
who give me this long error:
Existing instance variable 'window' for property 'window' with assign attribute must be __unsafe_unretained
and pratically the same for other var "webber":
Existing instance variable 'webber' for property 'webber' with assign attribute must be __unsafe_unretained
Thanks for your help, I really appreciate Stackoverflow community for days !!
The default ownership qualification for instance variables in ARC is strong, and like #robMayoff mentioned assign is the same as unsafe_unretained so your code reads like the following:
#interface webberAppDelegate : NSObject <NSApplicationDelegate> {
__strong NSWindow *window;
__strong WebView *webber;
}
#property (unsafe_unretained) IBOutlet NSWindow *window;
#property (unsafe_unretained) IBOutlet WebView *webber;
As mentioned in the linked answer provided by #Firoze, the property declaration and iVar should have matching ownership qualification. So the solution would be to make the __strong in the above code to __unsafe_unretained or to remove the instance variable declarations completely so that the compiler takes care of it.
The same solution is provided in the linked answer in the comment. Just adding some info.
I'm pretty new to objective c, and having some basic problems.
I wrote a simple program using a navigator, and everything worked fine.
then I added few lines of code (can't even remember what exactly, and it seems to have no connection to the problem) and the problem occurred. I tried ctrl+z, and the problem remained:
I run the program and get these errors:
1. unknown type name "mainController"
2. property with 'retain (or strong)' attribute must be of object type
while mainController is the first screen to be loaded.
This is the appDelegate.h file:
#import <UIKit/UIKit.h>
#import "mainController.h"
#import "WishesList.h"
#import "Wish.h"
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) IBOutlet UINavigationController *navController;
#property (strong, nonatomic) IBOutlet mainController *viewController; // this line creates the errors
#property (strong, nonatomic) WishesList *WishesArray;
#property (strong, nonatomic) NSIndexPath *temp;
#end
this is the relevant part of the appDelegate.m file:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
WishesArray = [[WishesList alloc]init];
temp = nil;
[self setViewController:[[mainController alloc]init]];
[self setNavController:[[UINavigationController alloc]initWithRootViewController:self.viewController]];
[self.window setRootViewController:navController];
[self.window makeKeyAndVisible];
return YES;
}
And this is mainController.h:
#import <UIKit/UIKit.h>
#import "addWishController.h"
#import "displayWish.h"
#import "WishesList.h"
#import "Wish.h"
#interface mainController : UIViewController
#property (nonatomic, weak) WishesList *list;
#property (nonatomic, strong) IBOutlet UITableView *wishTable;
- (void)addWish;
#end
it already worked...
can you figure it out?
thanks
I figured out, that the same error appears if you have an import-cycle:
Class_A.h: #import "Class_B.h"
Class_B.h: #import "Class_A.h"
To fix: look for any imports of the offending class (the error tab is your friend, expand the relevant error for a list of imports). Remove #import's accordingly
This problem happen to me once.
I was importing the "APPDelegate.h" in my h file and in my APPDelegate.h I was importing the file too (it shouldn't be a problem but...)
What I did: I changed the Import from my own .h to .m and it worked :)
As others have mentioned, this is indeed caused by cyclic imports. To fix this remove the imports in one of the classes. But sometimes this is not sufficient. If the classes depend on each other, simply forward-declare the one class in the other:
Class A:
#import <UIKit/UIKit.h>
#class B; //<- this is essential here
#interface A: NSObject
#property(nonatomic, strong) B *b;
//...
In class B we have:
#import "A.h"
#interface B: NSObject
#property(nonatomic, strong) A *a;
#JustAStranger and #NathanielSymer, both are correct!
Anyway, worth remember that this case, below, has the same problem too:
Class_A.h: #import "Class_B.h"
Class_B.h: #import "Class_C.h"
Class_C.h: #import "Class_A.h"
This problem reveal to us how important is take care about owners at our Class relationships. Is very easy creates cycle problems using ObjC headers.
Check the target and the files it is compiling. Perhaps mainController has some how been removed from that target. If so, when building, you would get the message that it cannot be found.
This problem looks like a typo because class names usually start with an uppercase character. Therefore, mainController could/should be MainController. Check the class name to see if the error is indeed a typo, because the compiler is telling you it cannot find any class called mainController.
I seem to be getting a new error when using LLVM Compiler 2.0, which I haven't had before.
I have a protocol called DTGridViewDelegate defined as:
#protocol DTGridViewDelegate <UIScrollViewDelegate>
I have a property called delegate on DTGridView (a subclass of UIScrollView, which itself has a delegate property). This is defined as:
#property (nonatomic, assign) IBOutlet id<DTGridViewDelegate> delegate;
Now the message I get is:
DTGridView.h:116:63: error: property type 'id<DTGridViewDelegate>' is incompatible with type 'id<UIScrollViewDelegate>' inherited from 'UIScrollView'
Because I had said that the DTGridViewDelegate conforms to UIScrollViewDelegate, I thought that this would be ok to override this property in this way, and indeed this is the first compiler to suggest there is a problem.
I have fixed the error by declaring the property as such:
#property (nonatomic, assign) IBOutlet id<DTGridViewDelegate, UIScrollViewDelegate> delegate;
I am wondering whether this is a compiler issue?
Your setup looks like the same one used in the case of UITableView inheriting from UIScrollView. The UITableViewDelegate protocol inherits from UIScrollViewDelegate protocol.
I set up the following which compiles fine:
// .h
#protocol ParentClassDelegate
-(NSString *) aDelegateMethod;
#end
#interface ParentClass : NSObject {
id delegate;
}
#property(nonatomic, assign) IBOutlet id <ParentClassDelegate> delegate;
#end
//.m
#implementation ParentClass
#synthesize delegate;
-(id) delegate{
return #"Parent delegate";
}//-------------------------------------(id) delegate------------------------------------
-(void) setDelegate:(id)someObj{
delegate=someObj;
}//-------------------------------------(id) setDelegate------------------------------------
#end
//.h
#protocol ChildClassDelegate <ParentClassDelegate>
-(NSArray *) anotherDelegateMethod;
#end
#interface ChildClass : ParentClass{
}
#property(nonatomic, retain) IBOutlet id <ChildClassDelegate> delegate;
#end
//.m
#implementation ChildClass
//#synthesize delegate;
-(id) delegate{
return #"childDelegate";
}//-------------------------------------(id) delegate------------------------------------
-(void) setDelegate:(id)someObj{
delegate=someObj;
}//-------------------------------------(id) setDelegate------------------------------------
#end
Not sure what is causing your problem. I would note that in the header the UITableViewDelegate protocol looks like:
#protocol UITableViewDelegate<NSObject, UIScrollViewDelegate>
... so maybe the compiler likes things more explicit sometimes.
I would suggest a clean and build. That solves a lot of problems.
Since there isn't a formal Objective-C language specification, it's impossible to say whether the compiler is behaving properly. All we can say is that Apple's gcc doesn't seem to have a problem with the above scenario, though it's conceptually unsound as it can break Liskov substitution, since delegate is covariant from UIScrollView to DTGridView (though covariance is just as much a problem). What would happen if you passed a DTGridView to code expecting a UIScrollView, which then proceeded to set delegate to an object that conformed to UIScrollViewDelegate but not DTGridViewDelegate?