Declare NSString in .h file - Objective c - ios

This may sound like a silly question, but I can'f find easily any solution online.
How to declare a NSString in .h file?
I have tried this:
#property (nonatomic, retain) NSString *DATABASE_NAME;
but I get an error:
Expected member name or ';' after declaration
What's the problem here?
EDIT
My header file:
#interface GlobalVariables : NSObject
#property BOOL MAP_SATELLITE_VIEW;
#property (nonatomic, retain) NSString *DATABASE_NAME;
+ (GlobalVariables*)sharedInstance;
#end

There's nothing wrong with the code you show, so you must look in its context. Some ideas:
Check how/where #import the .h file.
Check if DATABASE_NAME been defined elsewhere.
What happens if you replace this line with something else, for example: #property (nonatomic, assign) NSInteger dummyInt;, or add this above that line?

Related

Xcode: declaring private #property it complain (Objective-C)

I'm trying to declare a private property but I'm getting this error:
Unexpected '#' in program
Here is my implementation
#implementation MyClassImplementation
#property (nonatomic,strong) NSArray *new;
#end
Here is where I get the error #property (nonatomic,strong) NSArray *new; any of you knows why I'm getting this error or if there is a work around this?
I'll really appreciate your help
Private properties usually are declared in .m file in class unnamed category and for following Cocoa naming convention shouldn't use new keyword:
#interface MyClassImplementation ()
#property (nonatomic, strong) NSArray *array;
#end
#implementation MyClassImplementation
....
#end

Xcode Don't recognize Object anymore

I have an object named friend (I know, first letter should be uppercase). I use this class in many other classes and view controller.
All of a sudden, without changing ANY CODE, every custom object I have, stopped recognize each other. I always get the error Unknown type name "friend".
I already tried to clear the project and restart the mac.
WTF is wrong with this Xcode? All of a sudden my whole project stops working.
Here is my class currentUser.h
#import <Foundation/Foundation.h>
#import "friend.h"
#interface currentUser : NSObject<NSCoding>
{
BOOL fromfacebook;
#private
NSMutableArray *upLoadStack;
}
#property (nonatomic,strong)NSString *token;
#property (nonatomic,strong)NSString *email;
#property (nonatomic,strong)NSString *name;
#property (nonatomic,strong)UIImage *userImg;
#property (nonatomic,strong)NSString *username;
#property (nonatomic,strong)NSString *facebookID;
#property (nonatomic,strong)NSString *userPSW;
#property (nonatomic,strong)NSString *userID;
#property (nonatomic,strong)NSMutableArray *friendsList;
#property (nonatomic,strong)NSMutableArray *groups;
#property (nonatomic,strong)NSData *audioMessage;
#property (nonatomic,strong)NSMutableArray *mimosToDownload;
#property (nonatomic,strong)NSMutableArray *mimosDownloaded;
#property (nonatomic,strong)friend *friendToSend; //Here is where a I get the error of unknown type name
#end
this is the friend.h
#import <Foundation/Foundation.h>
#import "currentUser.h"
#import "Imager.h"
#interface friend : NSObject<NSCoding>
#property (nonatomic,strong)NSString *username;
#property (nonatomic,strong)NSString *userID;
#property (nonatomic,strong)UIImage *userImg;
#property(nonatomic,strong)NSMutableData *buffer;
#property(nonatomic)BOOL flagDownloaded;
-(UIImage*)downloadImageBlocked;
-(id)init;
-(UIImage*)getFriendImg;
-(UIImage*)userImg;
-(NSString*)getUserID;
-(NSString*)getUserName;
- (void)encodeWithCoder:(NSCoder *)encoder;
-(bool)isNil;
#end
fix the stuff that you know is wrong first...
change
#interface currentUser : NSObject<NSCoding>
{
BOOL fromfacebook;
#private
NSMutableArray *upLoadStack;
}
to
#interface User : NSObject<NSCoding>
{
BOOL fromfacebook;
#private
NSMutableArray *upLoadStack;
}
you don't need to know if the user is current or not for the user to have properties of a user, think of them as actually representing objects.
change:
#interface friend : NSObject<NSCoding>
to
#interface Friend : NSObject<NSCoding>
you know it is wrong, fix it now before you have to fix it in 1000 places instead of 100 places.
Convention is very important in Objective-C.
also don't import everything into your header if you don't need to...
#class Friend; //forward class declaration
#interface currentUser : NSObject<NSCoding>
...
#property (nonatomic,strong)Friend *friendToSend; //Here is where a I get the error of unknown type name
and as for your ivar's in currentUser you don't need those in the interface (they can go in a block after the #implimentation line), unless you need them for backwards compatibility...

objective c circular reference [duplicate]

This question already has answers here:
Errors when making circular reference imports
(2 answers)
Closed 8 years ago.
I have two custom NSObjects which reference each other, as below:
.h file class 1
#import <Foundation/Foundation.h>
#import "MyChildClass.h"
#interface MyParentClass : NSObject
#property (nonatomic, strong) NSString *Name;
#property (nonatomic) MyChildClass *myChild;
#end
.m file class 1:
#import "MyParentClass.h"
#implementation MyParentClass
#end
and...
.h file class 2:
#import <Foundation/Foundation.h>
#import "MyParentClass.h"
#interface MyChildClass : NSObject
#property (nonatomic) MyParentClass *myParent;
#end
.m file class 2:
#import "MyChildClass.h"
#implementation MyChildClass
#end
When I try and implement those as below, the project will not build, with the error "Arc Restrictions: Implicit conversion of an Objective-C pointer to 'int *' is disallowed with ARC"
View Controller implementation code:
MyParentClass *newParent = [[MyParentClass alloc] init];
MyChildClass *newChild = [[MyChildClass alloc] init];
newParent.Name = #"Johnny";
newParent.myChild = newChild;
newChild.myParent = newParent;
However, when I nest a second example into single .h/.m files, the project will build:
Nested .h file:
#import <Foundation/Foundation.h>
#class MyNestedClass;
#interface MyChildNestedClass : NSObject
#property (nonatomic, strong) MyNestedClass *myNested;
#property (nonatomic, strong) NSString *ChildName;
#end
#interface MyNestedClass : NSObject
#property (nonatomic, strong) MyChildNestedClass *myChild;
#property (nonatomic, strong) NSString *ParentName;
#end
Nested .m file:
#import "MyNestedClass.h"
#implementation MyChildNestedClass
#end
#implementation MyNestedClass
#end
I guess that by nesting the objects into a single file, I am somehow tricking ARC and that really I shouldn't be creating these circular references.
Could anyone suggest the correct way of nesting types like this?
What I am trying to achieve, is the following pseudo-scenario: Parent has a property of Child. Child has a property of the parent it is the child of.
Am I finding this difficult because I'm not supposed to do it (circular referencing), or am I missing something glaringly obvious?
Many thanks!
Do not #import "MyChildClass.h" in the parent class' header file, change it to
#class MyChildClass
and add the #import in the source file (.m)

iOS - Property not found on object of type X - inherited property

I have a CoreData-based app. In Core Data, I have an entity called ZSProduction which creates an NSManagedObject subclass called ZSProductionCD. This is the .h file created.
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#interface ZSProductionCD : NSManagedObject
#property (nonatomic, retain) NSNumber * appVersion;
#property (nonatomic, retain) id highlightColour;
#property (nonatomic, retain) NSDate * lastUpdated;
#property (nonatomic, retain) NSString * notes;
#property (nonatomic, retain) NSString * owner;
#property (nonatomic, retain) NSString * productionID;
#property (nonatomic, retain) NSString * productionName;
#property (nonatomic, retain) NSNumber * scenesLocked;
#property (nonatomic, retain) NSNumber * shotNumberingStyle;
#property (nonatomic, retain) NSNumber * sortIndex;
#property (nonatomic, retain) NSNumber * status;
#property (nonatomic, retain) NSString * tagline;
#end
I then subclass this with a class called ZSProduction:
#import <Foundation/Foundation.h>
#import "ZSProductionCD.h"
#interface ZSProduction : ZSProductionCD
#end
The reason for subclassing is that I am likely to add a bunch of methods & possibly other properties. This way, if I make changes to the entity, I can write out a new ZSProductionCD class without affecting what I've done with ZSProduction.
Now here's the problem. I'm using ZSProduction in a view controller. But I'm having a problem with just one of the properties.
In this view controller, I declare a property:
#import "ZSProduction.h"
#interface [...]
#property (strong, nonatomic) ZSProduction *item;
#end
And then later, in a method:
self.productionNameField.text = self.item.productionName;
self.shotNumStyleControl.selectedSegmentIndex = [self.item.shotNumberingStyle intValue];
And that's where it goes wrong. XCode complains:
Property 'shotNumberingStyle' not found on object of type 'ZSProduction *'
Note that it doesn't complain about the productionName property, which works fine.
In the same view controller, if I use:
self.item.shotNumberingStyle = 0;
Then I get the same error. But if I use:
[self.item setValue:0 forKey:#"shotNumberingStyle"];
Then it works fine. Yet I can use:
self.item.highlightColour = [UIColor whiteColor];
with no problem at all. What gives?
Any clues would be appreciated.
Well, I sorted the problem, but I thought I'd leave this here in case someone else has something similar.
I realised that the three properties that were being inherited okay, and were accessible, wee ones I'd created in an earlier version of the ZSProduction class - one that didn't use Core Data (I'm currently refactoring to move from archives to CD).
It turned out that there were old ZSProduction.h and ZSProduction.m files on the disk, in the project subdir - but not in XCode. Seems that XCode was looking at these. The old files were in the root dir for the project, while the newer files were in a subdir (called 'Model'). I deleted the old files, restarted XCode & all is now tickety-boo.

iOS: Saving to Data Structure

I would like to create an NSObject class that I can use the instance of and save to its variables and later pass its data elsewhere (NSManagedObject). Do I need to do anything else besides creating a new Object-C Class that inherits from NSObject. Create Variables in .h and synthesize in .m.
i.e.:
my .h file:
#import <Foundation/Foundation.h>
#interface MyDataClass : NSObject
#property (nonatomic, retain) NSNumber *variable1
#property (nonatomic, retain) NSString *variable2
#property (nonatomic, retain) NSDate *variable3
#end
.m file:
#import "MyDataClass.h"
#implementation MyDataClass
#synthesize variable1, variable2, variable3
#end
I would like to be able do the following in some SomeViewController:
#property (nonatomic, strong) MyDataClass *newDataClass
#systhesize newDataClass;
newDataClass.variable1 = #"123457890";
newDataClass.variable2 = #"This is the new Variable";
newDataClass.variable3 = [NSDate date];
Is there anything else I need to do to initialize each variable when an instance of this class is created? Am I missing anything?
That's all that you need for a custom object which you want to use to store data.
Of course when you go to use it (in SomeViewController), you need to actually create an instance of your class before you start setting the variables:
self.newDataClass = [[MyDataClass alloc] init];
I would also use self.newDataClass instead of just newDataClass unless you have a reason not to.

Resources