Proper way to make variable accessible within entire .m file? - iphone

As you can tell, I'm new to Objective-C. I currently have a Singleton working, but I'm trying to use it throughout several methods within the same .m file.
Right now I use this to declare/instantiate the Singleton within a method:
GlobalData *globDat=[GlobalData getInstance];
Do I need to declare it within each method, or is there a way to do this at the top of the .m (or .h?) file so I can access it throughout the other methods?
Thanks...
(BTW I've tried placing the line of code shown above under my "#implementation" line, but I get an error: "Initializer element is not a compile-time constant" which I now know is because the line is not within a method.)

You can add a class extension, and store the global data in ivar, like this:
#interface MyClass() {
GlobalData *globDat;
}
#end
#implementation MyClass
-(id) init {
self = [super init];
if (self) {
globDat=[GlobalData getInstance];
}
return self;
}
#end
This will hide the globDat from the interface, and make it available throughout the methods you implement inside your implementation block of MyClass.

when u need a variable / object that is accessible to all methods in an implementation data, u need to declare it on the header file, and then synthesize it on the implementation file (note that u may need to import the GlobalData header)
so
in header (.h):
#class GlobalData;
#properties (nonatomic, strong) GlobalData *globDat;
and then at the implementation file (.m)
#import "blablabla.h"
#import "GlobalData.h"
#implementation blablabla
#synthesize globDat;
-(void)viewDidLoad
{
globDat = [GlobalData getInstance];
}
good luck

Related

Objective C - Make iVars hidden

Here's my question.
Let's say I have a class called WebServiceBase.h. And I need to add a iVar in to that class called NSString *requestData. But I don't need to add that iVar in to the header file and make it visible to the external people. (If I'm distributing this as a class library)
Also I need to be able to access this requestData iVar, within the other classes that is extended from the WebServiceBase.h. (These extended classes are written by me. Not from the external people)
I tried with declaring the requestData iVar within the class extensions. But then it's not visible to the extended classes.
Any solution for this? I need to protect my data and make it hide from the external world.
You can define your ivars as protected via the #protected keyword, meaning that your class and all subclasses can access it without any problem, but the compiler won't allow this for other classes which don't inherit from your base class:
#interface Foo : NSObject
{
#protected
NSObject *a;
}
Its as simple as that and already gives you all the safety you can get from Objective-C.
You can have an ivar definition block in the #implementation block.
there are 2 ways , you can choose one you like.
1).h file
#interface YourClass
{
}
.m file
#interface YourClass ()
#property (nonatomic, retain) NSString *title;
#end
#implementation YourClass
#synthesize title;
//your method
2) .h flie
#interface YourClass
{
}
.m file
#implementation YourClass
{
NSString *title;
}
//your method
Declare a class extension that defines the ivar in another .h file. Call it something like myclass-private.h. Then import that header in both your main class your subclasses.

How to make a forward declaration for private method?

I'm arranging my methods into groups using #pragma mark in implementation. But sometimes, the method implementation code appears below the code that calls this method, and I'm getting "Instance method not found" warnings. It happens when I'm using private methods. How to fix that?
Simplest method is to use a anonymous category. Add something like this to the top of your .m file, before your #implementation:
#interface MyClass()
- (void)myPrivateMethod;
#end
In your Class.m implementation file, you can add an interface section at the beginning and declare private functions in there:
#interface YourClassName (private)
-(void)aPrivateMethod:(NSString*)aParameter;
...
#end
#implementation YourClassName
...
#end
In this case, you would use a class extension inside of your implementation file to define these methods. In this manner, your 'public' API is still defined in your header file, and your implementation file contains the definition of your pseudo-private methods.
YourClass.m
#interface MyClass()
- (void)myPrivateMethod;
#end
#implementation MyClass
- (void)myPublicMethod
{
// This will not throw an error or warning
[self myPrivateMethod];
}
- (void)myPrivateMethod
{
// Do something
}
#end

instance method of class for iphone

i want to create some header file for future use but i have 1 problem
i have defined a method in lets say Rimage class called check1
now i want to call that from maiviewcont
so i did this
in mainVC.h
i defined a instance of Rimage class
#import <UIKit/UIKit.h>
#class Rimage;
#interface Rahul_imageplaceCordinatesViewController : UIViewController {
Rimage *aRimage;
}
#property (nonatomic,copy) Rimage *aRimage;
#end
and in .m
[self.aRimage check1];
aRimage = [Rimage check1];
but both are not working
i went for both +(void)check1 and -(void)check1 in Rimage class
The two examples you give do very different things:
[self.aRimage check1];
invokes an check1 instance method on aRimage.
aRimage = [Rimage check1];
calls the check1 class method on the Rimage class, and assigns the result to aRimage.
In both cases, you will need to #import "Rimage.h in your .m file, else you'll get warnings like "aRimage may not respond to 'check1'".
EDIT
Your ".h" file is declaring a property named "aRimage", but that value will be nil until you assign something to it, by doing something like:
self.aRimage = [[[Rimage alloc] init] autorelease];
See this question for a good explanation of the difference between class methods and instance methods.
ps. and don't forget to do a [aRimage release] in your dealloc method

Object as a data member in Objective C

From what I have experienced it seems as if objects cannot be shared data members in objective c. I know you can init a pointer and alloc the object in each method but I cannot seem to figure out how one can say define a NSMutableString as a data member and allow all of the methods to use and modify its data as in c++. Is this true or am I missing something?
To define an instance variable (member), edit your .h file:
#interface MyClass : NSObject {
// ivars go here
NSObject *member;
}
// methods go here
#end
Then, in your .m file, from any instance method (one which begins with -), you can access this variable.
- (void)doThingWithIvar {
[member doThing];
}
If you want to access the variable from outside the object itself, you'll need accessors. You can do this easily with Obj-C properties:
#interface MyClass : NSObject {
// ivars go here
NSObject *member;
}
// methods go here
#property (nonatomic, retain) NSObject *member;
#end
And in the .m:
#implementation MyClass
#synthesize member;
// ...
#end
The #synthesize line creates getter/setter methods for the ivar. Then you can use property syntax:
MyClass *thing = ...;
NSLog(#"%#", thing.member); // getting
thing.member = obj; // setting
(Note that I specified (retain) for the #property; if your member isn't an Objective-C object you won't want that. And if your property's class has a mutable counterpart, you'll want (copy) instead.)
It sounds like you want to synthesize (create getter/setter methods) a property for a member variable. I just found this cheat sheet, go down to the section called, "Properties", should give a quick overview.
Other than that Apple's documentation should give you more info.

How do I call a variable from another class?

I have a class called 'Constants' that I am storing a String variable in. This class contains a few global variables used in my app.
I want to be able to reference this class and call the variable (called profileId) in other Views of my app.
I looked around and found a few examples, but am not sure how to do this. Currently my setup is:
Constants.h
#interface Constants : UIViewController {
NSString *profileId;
}
#property (nonatomic, retain) NSString *profileId;
#end
Constants.m
#import "Constants.h"
#implementation Constants
#synthesize profileId;
- (void)dealloc {
[profileId release];
[super dealloc];
}
And I am trying to call the variable profileId in a new View via this way:
NewView.h file
#class Constants;
NewView.m file
NSLog(#"ProfileId is:", [myConstants profileId]);
Is there something I'm missing? It is coming up null, even though I am properly storing a value in it in another function via this way:
Constants *Constant;
Constant = [[Constants alloc] init];
Constant.profileId = userId;
You are missing the %# for the parameter:
NSLog(#"ProfileId is: %#", [myConstants profileId]);
As a side note, variable names should begin with a lower case letter (constant, not Constant). You also can use dot syntax with properties here: myConstants.profileId
If this doesn't work, please post the code that you use to assign your value (complete method).