Why the code does not work to avoid Circular References? - objective-c

I am compiling code to avoid Retain Cycle, When I use weak , I got this error: 1. property of weak attribute must be of object type; 2. Unknown type name 'OrderEntry'. What is wrong with the code? Thanks!
// OrderEntry.h
#import <Foundation/Foundation.h>
#import "OrderItem.h"
#import "Address.h"
#interface OrderEntry : NSObject
#property (strong, nonatomic)OrderItem *orderItem;
#property (strong, nonatomic)Address *shippingAddress;
#property (strong, nonatomic) NSString *orderID;
#end
// OrderItem.h
#import <Foundation/Foundation.h>
#import "OrderEntry.h"
#interface OrderItem : NSObject
#property (strong,nonatomic) NSString *name;
#property (weak, nonatomic) OrderEntry *entry;
#end

The problem is with both .h files each including the other. This causes a circular dependency on the declarations. The simple solution is to use forward declarations instead.
OrderEntry.h:
#import <Foundation/Foundation.h>
#import "Address.h"
#class OrderItem;
#interface OrderEntry : NSObject
#property (strong, nonatomic) OrderItem *orderItem;
#property (strong, nonatomic) Address *shippingAddress;
#property (strong, nonatomic) NSString *orderID;
#end
OrderItem.h:
#import <Foundation/Foundation.h>
#class OrderEntry;
#interface OrderItem : NSObject
#property (strong, nonatomic) NSString *name;
#property (weak, nonatomic) OrderEntry *entry;
#end
Then you import the .h files in the .m file.
The general guideline is to import the fewest possible .h files in another .h file. Use forward declarations for classes and protocols whenever possible.

Related

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).

Objective-c: Using custom types in protocol member declaration

Please consider the following header (LessonDelegate.h):
#import "Lesson.h"
#ifndef BirdEye_LessonDelegate_h
#define BirdEye_LessonDelegate_h
#protocol LessonDelegate <NSObject>
- (BOOL) lesson:(Lesson*)lesson didRequestNavigation:(NSString*) url;
#end
#endif
It gives an error here:
- (BOOL) lesson:(Lesson*)lesson didRequestNavigation:(NSString*) url;
saying "Expected a type". It complains about Lesson type even if it is declared and successfully used in other classes.
Lesson.h is where it is supposed to be (same folder) and looks like this:
#import <Foundation/Foundation.h>
#import "SimpleManifest.h"
#import "Activity.h"
#import "LessonDelegate.h"
#interface Lesson : Activity
#property (retain, nonatomic) NSString *url;
#property (retain, nonatomic) SimpleManifest *manifest;
#property (retain, nonatomic, readonly) Activity *runningActivity;
#property (weak) id<LessonDelegate> delegate;
- (id) initWithUrl:(NSString*) aurl;
- (NSString*) deteails;
#end
Where's my mistake?
LessonDelegate.h
#class Lesson; // there was a missing ; here
#protocol LessonDelegate <NSObject>
- (BOOL) lesson:(Lesson*)lesson didRequestNavigation:(NSString*) url;
#end
Lesson.h
#import <Foundation/Foundation.h>
#import "SimpleManifest.h"
#import "Activity.h"
#import "LessonDelegate.h"
#interface Lesson : Activity
#property (retain, nonatomic) NSString *url;
#property (retain, nonatomic) SimpleManifest *manifest;
#property (retain, nonatomic, readonly) Activity *runningActivity;
#property (weak) id<LessonDelegate> delegate;
- (id) initWithUrl:(NSString*) aurl;
- (NSString*) deteails;
#end

objective-c 2.0 Duplicate interface definition for class when defining private methods

i try to define simple private methods and properties in class but it gives me:
Duplicate interface definition for class 'FBViewController'
this is the .m file :
#import "FBAppDelegate.h"
#import "FBViewController.h"
#import<FacebookSDK/FacebookSDK.h>
#import "FBLoginViewController.h"
#interface FBAppDelegate()
#property (strong,nonatomic) UINavigationController *navController;
#property (strong, nonatomic) FBViewController *mainController;
-(void) showLoginView;
#end
#implementation FBAppDelegate
#synthesize navController = _navController,
mainController =_mainController;
...
...
#end
and this is the *.h file:
#import <UIKit/UIKit.h>
#interface FBAppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#end
UPDATE
added the import files
*SOLVED *
in FacebookSDK there is FBViewController.h already
The error you are getting is
Duplicate interface definition for class 'FBViewController'
And you are looking to class called FBAppDelegate. Try reading the error message again, it may help.

Why am I getting unsafe retained?

I've just starting out with obj-c and I created 2 files, a .h and a .m file. The .h file is..
#import <Foundation/Foundation.h>
#interface CardUnit : NSObject
{
#private
NSString *_name;
NSString *_gold;
}
#property (nonatomic, assign) NSString *name;
#property (nonatomic, assign) NSString *gold;
#end
and the .m file is
#import "CardUnit.h"
#implementation CardUnit
#synthesize gold = _gold;
#synthesize name = _name;
#end
But it's giving me 2 errors on the #synthesize lines, which are...
"Existing ivar "_gold" for property gold with assign attribute must be __unsafe retained" and the same for name.
From the error i see you are using ARC, Automatic Reference Counting .
Basically you can get rid of all the #synthesize statements and even the private declarations of the ivar's name and gold is not necessary.
All you need is the CardUnit.h to be like this :
#interface CardUnit : NSObject
#property (assign) NSString *name;
#property (assign) NSString *gold;
#end
The Xcode compiler will take care of the rest.
See also this reply on SO

setting AVAudioPlayerDelegate to self gives me a warning

'__strong' only applies to objective-c object or block pointer types; type here is 'void *'
this the "__strong" line in my AVAudioPlayer.h file (which is an imported framework)
#interface AVAudioPlayer : NSObject {
#private
__strong void *_impl;
}
my .h file looks like this
#import <UIKit/UIKit.h>
#import <AVFoundation/AVAudioPlayer.h>
#interface excerViewController : UIViewController <AVAudioPlayerDelegate,UIAccelerometerDelegate>
#property (weak, nonatomic) IBOutlet UILabel *avgLabel;
#property (weak, nonatomic) IBOutlet UILabel *modeLabel;
#property (weak, nonatomic) IBOutlet UILabel *scoreLabel;
#property (weak, nonatomic) IBOutlet UILabel *timeLabel;
#property (weak, nonatomic) IBOutlet UIButton *playButton;
#property int subScore;
#property int score;
#property int seconds;
#property int moments;
#property NSMutableString *average;
#property double avg;
#property BOOL locked;
#property double delta;
#property UIAccelerometer *accel;
#property AVAudioPlayer *player;
-(void)drawEnergy:(double)energy;
#end
I can't for the life of me figure out why I'm getting this error. One other person on StackOverflow had this issue, and it isn't the reason i'm getting it
( '__strong' only applies to objective-c object or block pointer types; type here is XXX" warning)
Because void* is a pointer to an argument with no type, and not a true object, __strong does not apply to it.