Adding custom behavior and state to all my classes - iphone

I want to add functionality and another property to ALL of my classes.
So I wrote a category:
#implementation NSObject (MyCategory)
And I declared a static property in it:
static MyObj myObj;
And I created 2 class methods to get and set it:
+ (MyObj) getMyObj {
return myObj;
}
+ (void) setMyObj:(MyObj)obj {
myObj = obj;
}
Now I imported NSObject+MyCategory.h in my .pch file, so all classes will be effected by this. Indeed all classes now have the new functionality and state:
#import "NSObject+MyCategory.h"
The problem is that when I set myObj, it changes myObj on all classes. All classes share 1 myObj.
I want each class to have its own myObj that is added using the category. I don't want one myObj, rather I want as many myObj's as classes. Each class should have its own myObj.
Thanks,
Nur

You can not add properties instance variables to a class in categories. Either subclass NSObject or use associated objects.

Your solution adds a single static variable (not "property", in Objective-C that means something else), there is no way using categories to add a static variable per class.
However your idea is close to what will work for you; if you can only have one variable and want to store many values what can you use? A dictionary.
static NSMutableDictionary *References;
+ (void) load
{
// load is called exactly once when this category is first loaded,
// setup the dictionary
References = [NSMutableDictionary new];
}
+ (void) setMyObj:(MyObj)reference
{
// NSMutableDictionary will take any object reference as a key,
// for a class method self is a reference to the unique class object,
// so use self as the key and store the reference
[References setObject:reference forKey:self];
}
+ (MyObj) getMyObj
{
// returns previously stored reference or nil if there isn't one for this class
return [References objectForKey:self];
}
HTH

Related

is there a way to define a variable thats accessible to some classes and not others?

In objective-c, the access to variables is limited to three types which is #public , #private , #protected (default) and #package .. these access modifiers allow us to access the variable through 4 situations in order :
1- access the variable from anywhere.
2- access the variable only inside the class.
3- access the variable from anywhere in the class and its subclasses.
4- access the variable from anywhere in the framework.
my question is: is there a way to define a variable which is accessible to some classes and not others ? (i.e. customised scope for variables)
What you're asking for is C++'s friend keyword. Friend classes in Objective-C discusses the topic.
You can use class extensions to create more flexible access control:
// MyClass.h
#interface MyClass : SomeSuperclass {
int ivar;
}
#end
// MyClass-Custom.h
#include "MyClass.h"
#interface MyClass () {
int anotherIvar;
}
#end
Now anotherIvar will be accessible only to code that #includes MyClass-Custom.h. You can create more class extensions on the same class to get additional access groups.
You would have to write your own setter and getter methods.
- (id) get_abc_value:(id)from {
if ([from isKindOfClass:[SomeRespectedClass class]]) {
return abc;
}
return nil;
}

turn away to assign a property from Class method

I am just curious as if there is anyway to assign a property that is being triggered from class method?
Ex:
+ (void)asyncResponse:(NSDictionary*)response:(NSError*)error
I am getting a response of NSURLConnection in that call, however, I am trying to use this in my another class and set that dictionary as a property, but it gives me the error as this is a class method. That asyncResponse is a "Delegate call" I am using to direct to any particular class in runtime.
Thanks.
There are lot of options you may use to store the variable. Why do you need a property in class method. Class method is run when there is no role of instance. But, of course you, could pass create an instance of the class in the class method and store the value in the object. Other way would be to create some global variables and assign the values to it such that it can be accessed through out the class.
Creating instance to store the variable into property;
+ (void)asyncResponse:(NSDictionary*)response:(NSError*)error{
MyClass *me = [[MyClass alloc] init];
me.someProperty = response;
}
But, I dont think you were looking for this, because it is very simple use of the class and properties.
The other thing you could do is create some static variables inside your implementation and then access those variables through your class. You could even create your own custom getter and setter for it, for more easy uses.
#interface MyClass:NSObject
+(void)setResponse:(NSDictionary*)response;
+(NSDictionary*)response;
#end
NSDictionary *globalResponse;
#implementation MyClass
+(void)setResponse:(NSDictionary*)response{
if(response != globalResponse){
globalResponse = response;
}
}
+(NSDictionary*)response{
return globalResponse;
}
You could set the default value for the response in initialize or load method. This makes a simple class level property.

Calling a public method

I'm trying to figure out how to centralize a method that I use in a few of my ViewControllers. I already had a singleton that I was using for some variables. I called the singleton class Shared.
I moved my method to the Shared class and tried calling it like so:
m.createdAt = [Shared getUTCFormateDate:[messageObject objectForKey:#"created_at"]];
It's giving me an exception saying that the selector doesn't exist when it tries to call it.
I have already imported Shared.h. Any other thoughts would be appreciated.
If your class is named "Shared" then it looks like you are trying to call a class method rather than an instance method. So, you need to declare the method with + instead of -.
here is the correct pattern for creating a Singleton in objective-c: (Ill use an example of a User object.. taken from code I have open in front of me). also, please note that there is a difference between Singleton classes and Static Class methods, as discussed here.. Difference between static class and singleton pattern?
in the .h file, declare a static method that returns an instance of your class.
+(User *) currentUser;
in the .m file, create a static variable that holds your instance
static User * _user;
then, in your .m class, create your "public" static accesor GET that returns or instantiates and returns your static variable
+ (User *) currentUser
{
if (!_user)
{
_user =[[User alloc]init];
// ... init the singleton user properties and what not
// ...
}
return _user;
}
then, when you want to call on your Singleton class you just make sure that User.h is imported and call [[User currentUser] someMethodorProperty];
enjoy

How to use accessors within the same class in Objective C?

I have a few properties defined in my header file like so
#property (assign) bool connectivity_N;
#property (assign) bool isConnected_N;
In my implementation file I have an init and the synthesized properties like so
#implementation Map
#synthesize connectivity_N;
#synthesize isConnected_N;
a init to set the initial values like so
-(id) init
{
if( (self=[super init]) )
{
//initialise default properties
self.connectivity_N=NO;
self.isConnected_N=NO;
}
return self;
}
I'm running into an error that states Error: accessing unknown 'connectivity_N' class method. In this public method within the class
+(bool) isConnectable:(directions) theDirection{
bool isTheDirectionConnectable= NO;
switch (theDirection) {
case north:
isTheDirectionConnectable= self.connectivity_N;
break;
I'm not sure why is this so as I'm trying to grab the value of the property. According to the apple developer documentation "The default names for the getter and setter methods associated with a property are propertyName and setPropertyName: respectively—for example, given a property “foo”, the accessors would be foo and setFoo:"
That has given me a clue that I've done something wrong here, I'm fairly new to objective C so would appreciate anyone who spends the time to explain this to me.
Thanks!
You've defined isConnectable: as a class method by prefixing it with +. Probably you want it to be an instance method -- start it with a minus sign - instead.
You can't access self within class methods, because there is no object instance.
Although self exists in class methods, it doesn't refer to an object instance -- there is no object -- so you can't access object properties. (Thanks to Dave DeLong for the correction.)

iPhone static libraries: How to hide instance variable

I'm creating a static library to share using the following guide:
http://www.amateurinmotion.com/articles/2009/02/08/creating-a-static-library-for-iphone.html
In one of the functions, I return a "SomeUIView" which is a subclass of UIView and is defined in the public header, however I don't want to expose the internal instance variable of SomeUIView in the public header.
I've tried using categories for a private internal header file for SomeUIView, but I keep running into "Duplicate interface declaration for class 'SomeUIView'".
Does anyone know how to do this?
Thanks!
Categories and extensions can't add instance variables to a class. I'd go for the PIMPL idiom here - use a private implementation object:
// header
#class MyObjImpl;
#interface MyObj {
MyObjImpl* impl;
}
#end
// implementation file:
#interface MyObjImpl {
id someIvar;
}
// ...
#end
// ... etc.
This also keeps your public interface stable in case you want to add something for internal use.
The "duplicate interface" comes from missing parentheses in the second interface declaration:
// header:
#interface MyObj
// ...
#end
// implementation file:
#interface MyObj () // note the parentheses which make it a class extension
// ...
#end
You may also use the Objective-C 2 feature known as "Associative reference".
This is not really object-oriented API, but you can add/remove object to another object by using some simple functions of the runtime:
void objc_setAssociatedObject(id object, void * key, id value)
Sets the value or remove it when value is nil.
id objc_getAssociatedObject(id object, void * key)
Retrieve the value for specified key.
Note that this is also a mean to add "instance variable" to existing object when implementing a category.
Key is s simple pointer to private variable that you can declare as a module private by using:
static char SEARCH_INDEX_KEY = 0;