'Existing ivar 'delegate' for unsafe_unretained property 'delegate' must be __unsafe_unretained - objective-c

I'm getting the error above, but unsure how to go about fixing it. This is my code:
.h:
#import <UIKit/UIKit.h>
#protocol ColorLineDelegate <NSObject>
-(void)valueWasChangedToHue:(float)hue;
#end
#interface ColorLine : UIButton {
id <ColorLineDelegate> delegate;
}
#property (nonatomic, assign) id <ColorLineDelegate> delegate;
#end
.m:
#import "ColorLine.h"
#implementation ColorLine
#synthesize delegate;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
#end
The error occurs in the synthesize line. I can't find a problem though.

Use this syntax:
#interface SomeClass : NSObject {
id <SomeClassDelegate> __unsafe_unretained delegate;
}
#property (unsafe_unretained) id <SomeClassDelegate> delegate;

Looks like your project might be using ARC then properties should be declared this way:
#import <UIKit/UIKit.h>
#protocol ColorLineDelegate <NSObject>
-(void)valueWasChangedToHue:(float)hue;
#end
#interface ColorLine : UIButton
#property (nonatomic, weak) id <ColorLineDelegate> delegate;
#end

I had the same problem when I used old example code which did not feature ARC in my ARC project. It seems that you do not need to put the variable declarations into the interface definition any more. So your code should work like this:
h:
#import <UIKit/UIKit.h>
#protocol ColorLineDelegate <NSObject>
-(void)valueWasChangedToHue:(float)hue;
#end
#interface ColorLine : UIButton {
// Get rid of this guy!
//id <ColorLineDelegate> delegate;
}
#property (nonatomic, assign) id <ColorLineDelegate> delegate;
#end
.m:
#import "ColorLine.h"
#implementation ColorLine
#synthesize delegate;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
#end

Perhaps a bit late but to be "ARC compliant", you simply have to replace
#property (nonatomic, assign) id <ColorLineDelegate> delegate;
by
#property (nonatomic, strong) id <ColorLineDelegate> delegate;
Bye.

If you want a weak property, this also works.
#interface MyClass : NSObject {
__weak id <MyClassDelegate> _delegate;
}
#property (nonatomic, weak) id <MyClassDelegate> delegate;

you can also use
#dynamic delegate
in the implementation instead of synthesize.

Related

Calling a method from another class. error : No visible #interface ... declares the selector

I have two classes DetailViewController and Substitution, I would use the Substitution class methods but I get this error:
No visible #interface "Substitutions" declares the selector "Crypt ::"
DetailViewController.h
#import <UIKit/UIKit.h>
#import "Substitutions.h"
#interface DetailViewController : UIViewController
#property (weak, nonatomic) IBOutlet UITextField *tvC;
- (IBAction)bCrypter:(id)sender;
#property (nonatomic, strong) Substitutions * s;
DetailViewController.m
#import "DetailViewController.h"
#implementation DetailViewController
#synthesize s;
#synthesize tvC;
- (IBAction)bCrypter:(id)sender
{
s = [[Substitutions alloc]init];
[s Crypter:tvC.text :1]; <-- No visible #interface "Substitutions" declares the selector "Crypter::"
//NSLog(#"%#",tvC.text);
}
Substitutions.h
#interface Substitutions : NSObject
+ (NSString*)Crypter:(NSString*)msg :(id)cles;
#end
Substitutions.m
#import "Substitutions.h"
#implementation Substitutions
+ (NSString*)Crypter:(NSString*)msg :(id)cles
{
NSLog(#"%#",msg);
return #"";
}
Because Crypter is class method not instance method so you should call directly with class name not need to create instance for call method.
Ex.
[Substitutions Crypter:tvC.text :1];

property not found on object of type error but property is there

So I'm trying to access the property isPortClosed(BOOL) in SerialPortController and its giving me an error, I'm kinda new to objective-c. I feel like this should work as I've got a reference to the class with *port. Here is a link to the project.
Error messages: ~/GroundStation/GroundStation/ViewController.m:16:22: Property 'isPortClosed' not found on object of type 'SerialPortController *'
#import <Cocoa/Cocoa.h>
#import "SceneView.h"
#import "SerialPortController.h"
#interface ViewController : NSViewController
#property (strong) IBOutlet SerialPortController *port;
#property (weak) IBOutlet SceneView *accelSceneView;
#end
#import "ViewController.h"
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
while(!self.port.isPortClosed) {
}
}
- (void)setRepresentedObject:(id)representedObject {
[super setRepresentedObject:representedObject];
// Update the view, if already loaded.
}
#end
SerialPortController.h class
#import <Foundation/Foundation.h>
#import <ORSSerial/ORSSerial.h>
#interface SerialPortController : NSObject <ORSSerialPortDelegate>
#property (nonatomic, strong) ORSSerialPort *serial;
#property (nonatomic, strong) ORSSerialPortManager *serialPortManager;
#property (nonatomic) NSInteger xAngle;
#property (nonatomic) NSInteger yAngle;
#property (nonatomic) NSInteger zAngle;
#property (nonatomic) NSString *stringBuffer;
#property (nonatomic) BOOL isPortClosed;
#end
From the downloaded project I see that you have two SerialPortController class definitions (one at the root directory, and one in /GroundStation/), and the latter doesn't have any public properties. You should have only one SerialPortController class definition linked in your project (the one with the public properties).

Why doesn't my #property have an automatic getter?

I've declared a property called Squad, but when I send [self getSquad] I get "no visible #interface for SquadViewController declares the selector 'getSquad'".
SquadViewController.h:
#import "FlipsideViewController.h"
#import "Squad.h"
#interface SquadViewController : UIViewController <FlipsideViewControllerDelegate, UIPopoverControllerDelegate>
#property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
#property (strong, nonatomic) UIPopoverController *flipsidePopoverController;
#property (nonatomic, retain) IBOutlet UILabel *squadNameLabel;
#property Squad *squad;
- (IBAction)updateTitleWithName:(id)sender;
#end
SquadViewController.m:
#import "SquadViewController.h"
#interface SquadViewController ()
#end
#implementation SquadViewController
#synthesize squadNameLabel;
#synthesize squad;
...
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
if (![self getSquad]) //<--THIS IS WHERE THE ERROR IS
{
[self setSquad:[Squad squadWithName:#"New Squad"]]; //<-- NOT HERE, SO THE SETTER SEEMS TO EXIST
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
...
I thought that "#synthesize squad" would generate getSquad and setSquad, so I'm confused.
Here's the code for Squad, if for some reason that I don't get it's relevant (I'm new to Objective C, I still find it very confusing (I'm from a java background)):
Squad.h:
#import "SquadBuilderObject.h"
#interface Squad : NSObject
#property NSString *name;
+ (id) squadWithName:(NSString*)name;
#end
Squad.m:
#import "Squad.h"
#implementation Squad
#synthesize name;
+ (id)squadWithName:(NSString *)name
{
Squad *newSquad = [[Squad alloc] init];
[newSquad setName:name];
return newSquad;
}
#end
The standard getter for a property named squad is squad, not getSquad.
The "get…" nomenclature is typically reserved for things returned by reference (e.g. - (BOOL)getSquad:(Squad **)outSquad).

create control with properties within it

I have control something like this
#import <Foundation/Foundation.h>
#import "AQLevelMeter.h"
#import "AQPlayer.h"
#import "AQRecorder.h"
#interface SpeakHereController : NSObject {
IBOutlet UIBarButtonItem* btn_record;
IBOutlet UIBarButtonItem* btn_play;
IBOutlet UILabel* fileDescription;
IBOutlet AQLevelMeter* lvlMeter_in;
AQPlayer* player;
AQRecorder* recorder;
BOOL playbackWasInterrupted;
BOOL playbackWasPaused;
}
#property (nonatomic, retain) UIBarButtonItem *btn_record;
#property (nonatomic, retain) UIBarButtonItem *btn_play;
#property (nonatomic, retain) UILabel *fileDescription;
#property (nonatomic, retain) AQLevelMeter *lvlMeter_in;
#property (readonly) AQPlayer *player;
#property (readonly) AQRecorder *recorder;
#property BOOL playbackWasInterrupted;
#property BOOL isReport;
#property CFStringRef recordFilePath;
- (IBAction)record: (id) sender;
- (IBAction)play: (id) sender;
-(void) InitializeThePlayer;
#end
as we can see I added many properties like
#property BOOL isReport;
#property CFStringRef recordFilePath;
then I created uiview contains this control
#import <UIKit/UIKit.h>
#class SpeakHereController;
#interface SpeakHereViewController : UIViewController {
IBOutlet SpeakHereController *controller;
}
Now I want to access the properties of the control object so I say
- (void)viewWillAppear:(BOOL)animated {
[self ReportDirectory];
[controller setIsReport:self.iSReport ];
//controller.isReport = self.iSReport ;
[controller setRecordFilePath:(CFStringRef) self.DICOMpath];
//controller.recordFilePath = ;
}
the problem is that at the lines
[controller setIsReport:self.iSReport ];
[controller setRecordFilePath:(CFStringRef) self.DICOMpath];
there is warning say that
warning: no '-setIsReport:' method found
I made
#synthesize isReport;
#synthesize recordFilePath;
also if I replaced #class SpeakHereController; by #import "SpeakHereController.h" it raise a lot of errors , U can download the sample code from apple
and if I said controller.isReport = self.iSReport ; it raise error request for member 'isReport' in something not a structure or union
My question is how to call the properties in this control , am I missing something
Best regards
I tried
At the top of SpeakHereViewController.m you will need
import "SpeakHereController.h"
Otherwise when SpeakHereViewController.m is compiled, it is completely unaware of what methods and properties your SpeakHereController class has
It should run fine even with the warning because the property does exist. However, I agree with you that the warning needs to be dealt with.
You should synthesize those properties in .m file.
The syntax is like:
#synthesize isReport, recordFilePath;

inheritance working on simulator but not on Iphone!

I have come across a weird error...
I have a class that extends another, getting some variables from it. Its a really simple one.
when i compile the code for the simulator, it works great. When I run in the Iphone, it get an error that states that the vairable is not set!. The code:
parent class header:
#import <UIKit/UIKit.h>
#import "TarefaMap.h"
#interface GenericTarefaTableView : UITableViewController {
TarefaMap* tarefaMap;
TarefaMap* tarefaMapOriginal;
}
#property (nonatomic, retain) TarefaMap* tarefaMap;
#property (nonatomic, retain) TarefaMap* tarefaMapOriginal;
#end
parent class implementation:
#import "GenericTarefaTableView.h"
#implementation GenericTarefaTableView
#synthesize tarefaMap,tarefaMapOriginal;
#end
child class header:
#interface EditTarefaViewController : GenericTarefaTableView <UITextFieldDelegate , UITextViewDelegate> {
Tarefa* tarefa;
NSArray *fieldLabels;
UITextField *textFieldBeginEdited;
BOOL isEdit;
IBOutlet UITableViewCell *cell0;
IBOutlet UITextView* textView;
NSManagedObjectContext* context;
}
#property (nonatomic, retain) NSManagedObjectContext* context;
#property (nonatomic, retain) IBOutlet UITextView* textView;
#property (nonatomic, retain) IBOutlet UITableViewCell *cell0;
#property (nonatomic, retain) Tarefa* tarefa;
#property (nonatomic, retain) UITextField* firstField;
#property (nonatomic, retain) NSArray *fieldLabels;
#property (nonatomic, retain) UITextField *textFieldBeingEdited;
#end
Child class implementation:
#import "EditTarefaViewController.h"
#implementation EditTarefaViewController
#synthesize tarefa, textFieldBeingEdited,firstField,fieldLabels,textView, cell0, context;
NSMutableArray *textFieldarray;
UIActionSheet *actionSheet;
UITableViewCell* cell1;
- (void)viewDidLoad
{
NSLog(#"ViewDidLoad");
[super viewDidLoad];
self.tarefaMap=[[TarefaMap alloc] initWithTarefa:tarefa];
self.tarefaMapOriginal=[[TarefaMap alloc] initWithTarefa:tarefa];
if ([tarefaMapOriginal isEqual:tarefaMap]) {
NSLog(#"SOMOS IGUAIS!!!!!");
}
NSLog(#"Comparando tarefas!!!!!");
if ([tarefaMapOriginal isEqualtoTarefaMap:tarefaMap]) {
NSLog(#"SOMOS IGUAIS2!!!!!");
}
}
This compiles fine on the simulator, but when i Try on the IPhone, I get an error saying that the variables tarefaMap is undeclared, and should be declared on the function...
any thoughts?
#interface EditTarefaViewController : GenericTarefaTableView <UITextFieldDelegate , UITextViewDelegate> {
before that line add this
#class GenericTarefaTableView;
then in .m file add
import "GenericTarefaTableView.h"
and change like in viewdidload
NSLog(#"ViewDidLoad");
[super viewDidLoad];
super.tarefaMap=[[TarefaMap alloc] initWithTarefa:tarefa];
super.tarefaMapOriginal=[[TarefaMap alloc] initWithTarefa:tarefa];
#implementation EditTarefaViewController
before that u have to import TarefaMap.h and also declare this in EditTarefaViewController.h file with like this
#class TarefaMap;