Arc Error ?? Existing ivar delegate for unsafe_unretained property - xcode

I am currently following the tutorials in a book that was written before the use of arc in Xcode. I have the exact same problem like what is stated here:
'Existing ivar 'delegate' for unsafe_unretained property 'delegate' must be __unsafe_unretained
The question has been answered, but is has not been stated exactly why this is the case.
The Code in the book states that I use this code to declare member variables:
the.h File
___________
#import <Cocoa/Cocoa.h>
#interface TestProgramDelegate : NSObject <NSApplicationDelegate> {
NSWindow *window;
NSTextField *message;
}
#property (assign) IBOutlet NSWindow *window;
#property (assign) IBOutlet NSTextField *message;
#property (assign) IBOutlet NSTextField *inputText;
#end
the.m file
__________
#import "the.h" //File
#implementation theHFileAppDelegate
#synthesize window
#synthesize message
#synthesize inputText
When I synthesize the variables in my .m file, I get the following error message:
Existing ivar 'window' for property 'window' with assign attribute must be __unsafe_unretaied.
But when I remove the declaration of the member variables like its stated in the book, everything works fine and I don't get an error.
the.h File
___________
#import <Cocoa/Cocoa.h>
#interface TestProgramDelegate : NSObject <NSApplicationDelegate> {
NSWindow *window;
NSTextField *message;
}
#property (assign) IBOutlet NSWindow *window;
#property (assign) IBOutlet NSTextField *message;
#property (assign) IBOutlet NSTextField *inputText;
#end
the.m file
__________
#import "the.h" //File
#implementation theHFileAppDelegate
#synthesize window
#synthesize message
#synthesize inputText
My program works, but I want to understand why this causes trouble. Thanks a lot.

Related

View Switching: Unrecognised selector sent to instance

I made some example that changes view by clicking button.
When clicking the "fist" button in view, I keep getting the error:
[__NSArrayM changeLogView:]: unrecognized selector sent to instance 0x100539e30
Here is the simplified code:
DSWAppDelegate.h
#import <Cocoa/Cocoa.h>
#class DSWHomeViewController;
#interface DSWAppDelegate : NSObject <NSApplicationDelegate>
#property (assign) IBOutlet NSWindow *window;
#property (weak) IBOutlet NSButton *logBtn;
#property (weak) IBOutlet NSTabView *tabView;
#end
DSWAppDelegate.m
#import "DSWAppDelegate.h"
#import "DSWLogViewController.h"
#implementation DSWAppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
[self.tabView setTabViewType:NSNoTabsNoBorder];
NSTabViewItem *item = nil;
DSWLogViewController *logvc = [[DSWLogViewController alloc initWithNibName:#"DSWLogViewController" bundle:[NSBundle mainBundle]];
item = [[NSTabViewItem alloc] initWithIdentifier:#"log"];
[item setView:logvc.view];
[self.tabView insertTabViewItem:item atIndex:0];
}
#end
DSWLogViewController.h
#import <Cocoa/Cocoa.h>
#interface DSWLogViewController : NSViewController
#property (weak) IBOutlet NSButton *firstBtn;
#property (weak) IBOutlet NSButton *secondBtn;
#property (weak) IBOutlet NSBox *box;
- (IBAction)changeLogView:(id)sender;
- (IBAction)testSelector:(id)sender;
#end
DSWLogViewController.m
#import "DSWLogViewController.h"
#implementation DSWLogViewController
- (IBAction)changeLogView:(id)sender
{
NSLog(#"changeLogView : %#", sender);
}
- (IBAction)testSelector:(id)sender
{
NSLog(#"testSelector : %#", sender);
}
#end
And I checked class name of file owner.
Something is sending a changeLogView: message to a mutable array instead of your view controller. Chances are, whatever you've connected the IBAction to is pointing to the wrong object - an array of some sort, rather than your view controller.

Xcode Issue can't get code working

When I tried to execute this XCODE an error occured. Can anyone help me understand it please? The Error is at line 5.
#import <UIKit/UIKit.h>
#interface BIDViewController : UIViewController
#property (weak, nonatomic) IBOutlet UITextField *numberField;
#property (weak, nonatomic) IBOutlet UITextField *nameField;
- (IBAction)textFieldDoneEditing:(id)sender;
[sender resignFirstResponder];
#end
Thanks.
[sender resignFirstResponder];
You can't do that in a .h file. Remove it. That belongs in .m.
in .h
#import <UIKit/UIKit.h>
#interface BIDViewController : UIViewController
#property (weak, nonatomic) IBOutlet UITextField *numberField;
#property (weak, nonatomic) IBOutlet UITextField *nameField;
- (IBAction)textFieldDoneEditing:(id)sender;
in .m
- (IBAction)textFieldDoneEditing:(id)sender
{
[sender resignFirstResponder];
}

No declaration of property ... found in interface - But there is

I have the following error: No declaration of "window" found in interface.
Though when I look there is one... There's probably something stupid I missed, but can't find it.
PlanetoidsAppDelegate.h
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#interface WebViewExampleAppDelegate : NSObject {
NSWindow *window;
IBOutlet WebView *webView;
}
#property (assign) IBOutlet NSWindow* window;
#property (nonatomic, retain) IBOutlet WebView* webView;
#end
PlanetoidsAppDelegate.m
#import "PlanetoidsAppDelegate.h"
#implementation PlanetoidsAppDelegate
#synthesize window; //<-- error here
#synthesize webView; //<-- error here (well, the same one for webView that is...)
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
}
- (void)awakeFromNib {
NSString *resourcesPath = [[NSBundle mainBundle] resourcePath];
NSString *htmlPath = [resourcesPath stringByAppendingString:#"/Planetoids/Planetoids_Game.html"];
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:htmlPath]]]; //<-- error: webView undeclared, what makes sense considdering the other errors :P
}
#end
Can anyone here see the error?
Your interface is WebViewExampleAppDelegate but your implementation is PlanetoidsAppDelegate. These must match.
#interface WebViewExampleAppDelegate : NSObject in .h
#implementation PlanetoidsAppDelegate in .m
Two completely different classes.You need to be implementing WebViewExampleAppDelegate.m and synthesizing those methods in that class.
Also, for this:
#interface WebViewExampleAppDelegate : NSObject {
NSWindow *window;
IBOutlet WebView *webView;
}
try
#interface WebViewExampleAppDelegate : NSObject {
UIWindow *window;
IBOutlet WebView *webView;
}

Updating a text field from the URL from WebKit

First off let me say that I am VERY new to COCOA but I am learning. I have a simple web browser built. It loads a default page, takes URLs from a text field, even has google search. The problem I'm running in to is updating the textfeild with the current URL. I have the code as follows...
In the .h is...
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#interface MustangAppDelegate : NSObject <NSApplicationDelegate> {
NSWindow *window;
NSTextField *googleSearch;
WebView *webView;
NSTextField *addressBar;
}
#property (assign) IBOutlet NSWindow *window;
#property (nonatomic, retain) IBOutlet NSTextField *googleSearch;
#property (nonatomic, retain) IBOutlet WebView *webView;
#property (nonatomic, retain) IBOutlet NSTextField *addressBar;
- (IBAction)googleSearch:(id)sender;
- (IBAction)homeAction:(id)sender;
- (IBAction)addressBar:(id)sender;
#end
In the .m I have a bunch so I'll put in what I think is relevant here...
#import "MustangAppDelegate.h"
#implementation MustangAppDelegate
#synthesize window;
#synthesize googleSearch;
#synthesize webView;
#synthesize addressBar;
- (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
NSURLRequest *currentRequest = [webView request];
NSURL *currentURL = [currentRequest URL];
addressBar.stringValue = currentURL.absoluteString;
}
The code for the above method never get's called. Like I said, I'm very new to OBC & Cocoa but have been programming in things like Perl for many years. Seems like I'm missing some connectivity because I get a warning with the above code that states WebView may not respond to the request. I dug around to find that code but it still seems like I'm missing something...
Thanks
Mike
Just CTRL + drag the -frameLoadDelegate from the IB to the App Delegate of your application, Mustang
use -didFinishLoadForFrame for the URL
and if you need the title use, -didRecieveTitle.
Hope it helped !

objective C: may not respond to (issue)

I have two UINavigationController in the appdelegate.h
{
UINavigationController *leftView;
UINavigationController *rightView;
UIWindow *window;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UINavigationController *leftView;
#property (nonatomic, retain) IBOutlet UINavigationController *rightView;
appdelegate.m
#synthesize leftView;
#synthesize rightView;
then in a different class
test.m
#import "appdelegate.h"
if I do:
[self leftView] pushViewController...; //(Everything is ok)
but if I change it to:
[self rightView] pushViewControll...; //it complains about ViewController may not respond to -rightView
You code seems right; what doesn't seem right is that you're including AppDelegate. If the code was in AppDelegate.m, it would probably work without any complaints by the compiler.
Instead, you've defined #property lines for each, but that is in AppDelegate.h, not test.h - but you're using them in test.m. That is probably the source of the problem.
Interestingly, does the code actually run?
I'm going to hazard a guess: Have you declared and synthesized a property for rightView, or at least provided your own manual getter?
It sounds like you declared a #property or method called leftView that returns the leftView ivar, but you forgot to do the same for rightView. The error is telling you that self doesn't respond to the selector rightView.

Resources