Declaring primitive type properties - iphone

I want to declare properties with ints and bools, for example:
#property(nonatomic,retain) bool signOutgoingFax;
The error I get is:
property 'signOutgoingFax' with 'retain' attribute must be of object type

You do not retain BOOL int or float. Simply use
#property(nonatomic) bool signOutgoingFax;
The point here is that the variable is declared as "BOOL", not "BOOL *" (this would be a pointer), and hence you should not use retain.

Related

How to define typedef for a range of float numbers?

I want a property that will only accept float values between 0 and 1. I could do that in a standard way by defining float_t or double_t type for the property, but doubting, are there more elegant ways for that?
Do it the standard way - define a double/float property and check for validity in the setter - example:
#property (nonatomic, assign, readwrite) float property;
#synthesize property = _property;
- (void)setProperty:(float)property {
NSAssert(property >= 0.0f && property <= 1.0f, #"Invalid value passed to property setter.").
_property = property;
}
(Objective-)C doesn't support subrange types. For that you need Ada (or a few others). The best you can do is implement checks in your setters.

warning when using BOOL variable in objective-c

I am trying to initalize my BOOL variable to YES but its giving me this warning.. not quite sure what to do.. it still seems to be working fine but just wondering how I can get rid of the warning.
I have initalize the variable in the header like this
//.h
BOOL *removeActivityIndicator;
//..
#property (nonatomic, assign) BOOL *removeActivityIndicator;
Then I try to set it to YES like so (this is also where I get the warning)
self.removeActivityIndicator = YES;
The warning says :
incompatible integer to pointer conversion passing 'BOOL' (aka
'signed char') to paramater of type 'BOOL *' (aka 'signed char *')
The warning is correct; you've declared the variable as a BOOL * (a pointer to a BOOL), which is almost certainly not what you want. Remove the * from the declaration.
removeActivityIndicator is a char pointer, and you assigns a char to it, so either:
change it to be BOOL removeActivityIndicator;
Dereference it: *(self.removeActivityIndicator) = YES;
You've made a pointer to a BOOL, which is a primitive type. Remove the extra * in front of remoteActivityIndicator.

Objective C - Getter and setter properties for enum

I am a complete newbie at Objective-C. I have an enum as follows:
typedef enum _XLBadgeManagedType {
XLInboxManagedMethod = 0,
XLDeveloperManagedMethod = 1
} XLBadgeManagedType ;
I want to have getter and setter methods for it, such that if something happens, I set XLInboxManagedMethod to 1. How would I go about doing it?
Your code is just defining an enum type. It's a static, compile-time constant that is not changed. You use enums by declaring an instance of one, then changing it to one of the constant values you defined. If your enum looks like:
typedef enum _XLBadgeManagedType {
XLInboxManagedMethod = 0,
XLDeveloperManagedMethod = 1
} XLBadgeManagedType;
Then your property could look like:
#property (nonatomic, assign) XLBadgeManagedType myEnum;
And its use may look like:
- (void)someMethod {
self.myEnum = XLInboxManagedMethod;
self.myEnum = XLDeveloperManagedMethod;
// etc...
}
You do not change the values of enums. They stay as they are.
They are symbolic constants. You can not change it.

How can I pass a property of a class as a parameter of a method in objective-c

How can I pass a property of a class as a parameter of a method in objective-c?
So as an example assume I have:
a CoreData managed object class MyData with dynamic properties PropA, PropB, PropC all of the same type
I have a utils method that will perform calculations and update one of these properties, which takes as input the MyData instance
how can I arrange so the utils method can accept an indication of which property to use in the calculations and updating? (e.g. PropB)
So then need:
A way to pass an indication of the property to the method (e.g. send as String?)
A way in the method to take this (from 1 above) and use this to both (a) access the value of this property in the MyData instance the method has, PLUS (b) update the property too.
A properties will have setter and getter method. In you case, I assume there are setPropA, setPropB, setPropC for setters and PropA, PropB, PropC for getters.
Then I pass string "PropA" to util, indicate I want to access property named PropA.
The util can get the value by
id val = [aObj performSelector:NSSelectorFromString(#"PropA")];
And set the property by
[aObj performSelector:NSSelectorFromString(#"SetPropA") withObject:newValue];
Or, You can pass setter and getter as parameter by NSStringFromSelector(), turn selector into a NSString. For example, I pass setter and getter by NSDictionary.
NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
NSStringFromSelector(#selector(setPropA:)), kSetterKey,
NSStringFromSelector(#selector(PropA)), kGetterKey, nil];
// inside myUtil
NSString *setter = [userInfo objectForKey:kSetterKey];
[aObj performSelector:NSSelectorFromString(setter) withObject:newValue];
NSString *getter = [userInfo objectForKey:kGetterKey];
id val = [aObj performSelector:NSSelectorFromString(getter)];
Hope this helps.
Yes, you can pass the property name as a String.
Then you can access the indicated property via Key-Value Coding:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/KeyValueCoding/Articles/KeyValueCoding.html
Example:
- (void) myUtilMethod: (MyData *) myData
forPropertyNamed: (NSString *) propName /* which property to operate on (1) */
{
id oldValue = [ myData valueForKey: propName]; // get value (2a)
id newValue = ...; // your calculation here
[myData setValue: newValue forKey: propName]; // set value (2b)
}
I won't bind the 2 classes directly. You should set up a pattern design that allows you to loosely couple them together by creating a class that will do the interface between those two.

Convert string into variable

If in a class, I have a instance variable: nsstring *foo, now I want a create a variable with a string #"foo".
Example: I have the string #"foo", and with this string, I want a do: myobject.foo.
I think what you are looking for is KVC Key-Value Coding.
It enables you to retrieve or set a property or an instance variable on an object by it's name (a String). You can do something like:
id fooValue = [myobject valueForKey:#"foo"];
[myobject setValue:barValue forKey:#"foo"];
If you have setter and getter methods (or have synthesized them), you could use NSSelectorFromString() to access and modify your variable.
Accessing:
id foo = [self performSelector:NSSelectorFromString(#"foo")];
Modifying:
[self performSelector:NSSelectorFromString([NSString stringWithFormat:#"set%#:, [#"foo" capitalizedString]]) withObject:foo];