Accessing a global varibile in any class - iphone

Hey all i have been trying to figure out why i am getting this warning:
'TxtAppDelegate' may not respond to '-TCN'
'TxtAppDelegate' may not respond to '-TID'
when i try to use this code:
// .h file
#interface RootViewController : UITableViewController <UIActionSheetDelegate> {
NSString *theCompanyName;
NSString *theID;
}
#property (nonatomic, retain)NSString *theCompanyName;
#property (nonatomic, retain)NSString *theID;
// .m
NSString *theCompanyName;
NSString *theID;
#synthesize theCompanyName;
#synthesize theID;
TxtAppDelegate *customObjInstance = [[TxtAppDelegate alloc] init];
theCompanyName = [customObjInstance TCN];
theID = [customObjInstance TID];
I've added the header for the .h file that has the two functions in them. The code works but i really would like to solve the warning problem.
Any help would be great to solve this problem of mine :)
David

While it would have been more helpful to see the header file where TxtAppDelegate is declared, I'm guessing the method declarations must be off. They should look like this:
- (NSString *)TCN;
- (NSString *)TID;
If this is not the cause of the problem, please post the header file here so we can examine it.

How are declared these two functions in your header?
They should belong to a category of TxtAppDelegate class or to a protocol. If you choose the protocol, TxtAppDelegate interface should mention that it conforms to that protocol.

Related

private and public property in objective-C

So what is actually the difference between these two versions:
#interface Foo : NSObject
// A guy walks into a bar.
#property(nonatomic, copy) NSString *bar;
#end
// Implementation file
#interface Foo ()
#property(nonatomic, retain) NSArray *baz;
#end
and
#interface Foo : NSObject
// A guy walks into a bar.
#public
#property(nonatomic, copy) NSString *bar;
#private
#property(nonatomic, retain) NSArray *baz;
#end
As far as my understanding goes, putting the #property in the .m basically means that it is private. Correct me if I am wrong? Also which is the best implementation then? Is it just a coding style/practice?
The compiler can warn you about things that it knows about.
When I import your header the compiler can see that Foo has a method called bar and setBar:. This means I can use them both
[instanceOfFoo setBar:#"some string"];
NSLog(#"%#", [instanceOfFoo bar]);
whereas because I only imported the header - the compiler can only see the header it is not aware that there are also methods baz and setBaz: available, so doing the following will cause the compiler to barf
[instanceOfFoo setBaz:#"some string"];
NSLog(#"%#", [instanceOfFoo baz]);
I can however still access these properties if I know they exist by using KVC like this without the compiler barfing
[instanceOfFoo setValue:#"some string" forKey:#"baz"];
NSLog(#"%#", [instanceOfFoo valueForKey:#"baz"]);
You are correct in your understanding. Putting the #property in an #interface in the .m is making it "private". What that means is you'll get compiler warnings if you try to access that property from another class that includes the .h that doesn't include the #property declaration. This doesn't mean that you can't access the property, just that the compiler will yell at you.
As for best, neither one is best. You should implement the one that makes sense for you object, which could include items in both the .h and .m (read only proper in .h with full property in .m). Basically if the #property shouldn't ever be accessed outside of your class put it in the .m.

Lexical or Preprocessor Issue error causing Core data class "may not respond to" method warnings?

I get Lexical or Preprocessor Issue 'Group.h' file not found which I believe is causing the issues below.
I'm trying to call a method on one of my core data class instances and I get a 'Group' may not respond to '-addPeople:' warning. But I do have an addPeople method in my XCode generated Group class, and here it is:
- (void)addPeople:(NSSet *)value {
[self willChangeValueForKey:#"people" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
[[self primitiveValueForKey:#"people"] unionSet:value];
[self didChangeValueForKey:#"people" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
}
I also get the same warning if I try to removePeople. Both of these methods have to do with NSSets but I'm able to call my setters from the Group class just fine.
[selectedObject setTitle:[[titleCell textField] text]]; // works
[selectedObject setSubtitle:[[subTitleCell textField] text]]; // works
NSSet *tempPeople = [NSSet setWithArray:people];
[selectedObject addPeople:tempPeople]; // works, but with warning
Side note
When I type [selectedObject I don't get autocompletion, although the word selectedObject does autocomplete as a Group. So at least that's good.
Group.h
#class People;
#interface Group : NSManagedObject {
#private
}
#property (nonatomic, retain) NSString * title;
#property (nonatomic, retain) NSNumber * order;
#property (nonatomic, retain) NSString * subtitle;
#property (nonatomic, retain) NSSet* people;
#end
Updated
I just finished making a new UITableViewCell class and now instead of it saying Lexical or Preprocessor Issue 'Group.h' file not found it now says Lexical or Preprocessor Issue 'PersonCell.h' file not found
It would be helpful if you were able to post your interface file as well. These are a few reasons I might see for this:
You HAVE a Group.h file, but it does not have a method declaration for these methods.
You HAVE a Group.h file, it does have method declarations, but you are not #importing the Group.h into the other class.
You do NOT have a Group.h file at all, and are attempting to configure the method and variables as private objects in the implementation file.
Can you post more of your code for us? Possibly the [selectedObject] declaration as well?
Lexical or Preprocessor Issue
Well, I couldn't figure out what my problem was so I made a new project and remade each view controller, core data and whatever else I needed. And that solved my problem. I wish I knew what was really causing the Lexical error but from the looks of other people's posts, it could have been a bug.
Group' may not respond to '-addPeople:
This issue was resolved by adding the methods to the .h file. I'm not sure why XCode didn't make those for me, but that solved that problem.
And thanks xianritchie for trying to figure this out with me. It was appreciated.

Reach another class variable or control in iphone

Hi i will try to explain my problem: i'm working with webservices and i need to give variable or label.text to webservice request wich is in another class... I tried in lots ways, but i can't find solution. I know it must be very easy, but i'm new in objective-c so i have problem in primitive situation. Thanks for everyone who will help me :)
Use an instance variable and a property. See here.
In .h:
#interface YourClass : Superclass {
NSString *text;
}
#property (nonatomic, retain) NSString *text;
in .m you have to synthesize it:
#implementation YourClass
#synthesize text;
now you can set this variable like so:
[instanceOfYourClass setText:someText];
get it like so:
someString = [instanceOfYourClass text];
and use it like so (in your implementation):
someString = [self text];

Access a NSMutableArray of an object

I post this topic because I have a problem with my iPhone application since 3 days. I hope someone can help me because I'm going crazy.
Here is the thing : I fill in an object userXMLData,in the delegate of my application, with a XML Parser. This object contains many NSStrings and a NSMutableArrays which contains objects type Album to.
My problem is : I can display all data of userXMLData with an internal function, but when I'm trying to get the data from the array in my viewController , it doesn't work. I mean, it crashes. It's weird because I can access to the appDelegate.userXMLData.NSString but not of my appDelegate.userXMLData.NSMutableArray
Here is my code :
// Initializaiton in the delegate
userXMLData = [[UserXMLData alloc] init];
userXMLData.myArray = [[NSMutableArray alloc] init];
UserXMLData.h
#interface UserXMLData : NSObject {
// User Data
NSString *userId;
// Content
NSMutableArray *myArray;
}
#property(nonatomic, retain) NSString *myString;
#property(nonatomic, copy) NSMutableArray *myArray;
#end
//Album.h
#interface Album : NSObject {
NSString *albumId;
NSMutableArray *content;
}
#property(nonatomic, retain) NSString *albumId;
#property(nonatomic, retain) NSMutableArray *content;
#end
As I said, I really don't why it crashes. I'm stuck and I cannot continue my application without fixing it.
Enable Zombies by following the instructions here:
http://loufranco.com/blog/files/debugging-memory-iphone.html
This will cause your app to not release any objects and instead cause them to complain to the console if messages are sent to them after they are released.
The most common cause of a crash is releasing too often (or retaining too few times).
Also, running a Build and Analyze can sometimes point these out.
Would be able to answer better if you'd show the code where you are trying to access the array and the error you receive on crash, but I'd hazard a guess that you don't have #synthesize myArray in your implementation (.m) file

Stumped: Cannot use class definition

I have a Owners class defined that is used just fine in existing classes.
I tried to add another class, and no matter what I do (deleted, recreated new class, copied other class files over and renamed, cleaned all targets, howled at the moon), I cannot use this particular class in any new files I create.
The class is simple with a singleton definition of an array.
Here it is in use in another class (just fine):
#import "OwnersSchema.h"
#import "Owners.h"
#import "Constants.h"
#implementation OwnersViewController
#synthesize mid,imageStore;
- (OwnersSchema *)OwnersForIndexPath:(NSIndexPath *)indexPath {
return [[Owners sharedOwners].ownersArray objectAtIndex:indexPath.row];
}
So I make an exact copy of the above header and make files, just rename interface, etc. and add the same exact code, and it claims Owners is undeclared: First use in function.
I can add other class definitions to the new file and it sees them just fine.
Here is the Owners class:
#import <UIKit/UIKit.h>
#import "URLConnection.h"
#class OwnersSchema;
#interface Owners : NSObject <URLCacheConnectionDelegate> {
NSMutableArray *ownersArray;
NSString *lastUpdated;
NSString *url;
NSString *dbpath;
}
#property (nonatomic,retain) NSString *dbpath;
#property (nonatomic,retain) NSMutableArray *ownersArray;
#property (nonatomic,retain) NSString *lastUpdated;
#property (nonatomic,retain) NSString *url;
-(void)saveOwnersArray;
+ (Owners *)sharedOwners;
#end
I don't get it. I just want to create another viewcontroller. Is there some limitation on the number of files or class inclusions (actually, I use this Owners class in only a few places). And why does it work everywhere else except for the new class I'm trying to create?
Try a clean and rebuild?
OK, I cleaned all targets again, then ran clean caches, then restarted, and the class is usable again.