Why should I not use object_setIvar to set properties - ios

I have a dictionary containing data for user from a REST endpoint. Here is my user class
#import <Foundation/Foundation.h>
#interface User : NSObject
#property (strong, nonatomic) NSString *uid;
#property (strong, nonatomic) NSString *email;
#property (strong, nonatomic) NSString *firstName;
#property (strong, nonatomic) NSString *lastName;
#property (assign, nonatomic) int status;
#end
I have a method to set all properties
/*
* set properties
*/
- (void)setPropertiesWithDictionary:(NSDictionary *)dictionary{
for(NSString *key in dictionary){
object_setIvar(self,
class_getInstanceVariable([self class], [[#"_" stringByAppendingString:key] UTF8String]),
dictionary[key]);
}
}
Instead of doing something like
[user setUid:#dictionary[#"uid"]];
I want to call my method like this
[user setPropertiesWithDictionary: dictionary];
Just wondering if implementing object_setIvar this way is fine. If not - Would be really great if you can explain why. Thanks in advance.

Do whatever you like, but why reinvent the wheel when key value coding (KVC) already exists? Just call this method:
https://developer.apple.com/documentation/objectivec/nsobject/1417515-setvaluesforkeyswithdictionary?language=objc
KVC does what you're trying to do, but it does it a lot better than you're likely to do it.
https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/KeyValueCoding/index.html

I think your problem might occur with "int status", because dictionary[# "status"] is not of type int.
In your code implementation,user.status = dictionary[# "status"],this result is unpredictable.
Unless you make a type judgment, user.status = [dictionary[# "status"]intValue];
I recommend a third-party framework on github called MJExtension that fulfills your needs.You can look at the source code.

Related

making one property strong,nonatomic for objective-c

I have a multi view application and use an object to keep track of my logged in user. My User.h looks like this
#interface User : NSObject
#property (strong, nonatomic) NSDictionary *data;
#property (weak, nonatomic) NSString *uid;
#property (weak, nonatomic) NSString *firstName;
#property (weak, nonatomic) NSString *lastName;
#property (weak, nonatomic) NSString *dob;
#property (weak, nonatomic) NSString *gender;
#property (weak, nonatomic) NSString *avatarURL;
#property (assign, nonatomic) NSInteger status;
- (void)setPropertiesWith:(NSDictionary *)data;
And the User.m looks like this
#import "User.h"
#implementation User
/*
* set properties
*/
- (void)setPropertiesWith:(NSDictionary *)data{
self.data = data;
self.uid = self.data[#"uid"];
self.firstName = self.data[#"firstName"];
self.lastName = self.data[#"lastName"];
self.dob = self.data[#"dob"];
self.gender = self.data[#"gender"];
self.status = [[self.data valueForKeyPath:#"status"] intValue];
self.avatarURL = self.data[#"avatarURL"];
}
#end
I had the data as weak, but in one of the views it would turn up null - I believe ARC was releasing it. Please correct me if I am wrong.
I have 2 questions:
With this setup, the data being strong and the rest of the properties being weak, is there any potential risk to this?
Should I make the data an ivar and keep the rest as is?
There is no actual reason(other than my poor class design skills) for the existence of the properties. I just find it very interesting and wanted to understand what is going on.
You asked:
With this setup, the data being strong and the rest of the properties being weak, is there any potential risk to this?
Yes, if you nil the dictionary, all of your properties would likely become nil, assuming you don’t have other strong references to them elsewhere.
Should I make the data an ivar and keep the rest as is?
I wouldn’t even make it an ivar (unless there’s some other requirement for saving this that you haven’t shared with us). It should just be a local variable, and make your properties copy (or strong).
I’d suggest (a) getting rid of the NSDictionary property and (b) making the NSString properties be copy (or strong), not weak. Also, rather than having a setPropertiesWith method, I’d just define an initializer:
// User.h
#interface User : NSObject
#property (copy, nonatomic) NSString *uid;
#property (copy, nonatomic) NSString *firstName;
#property (copy, nonatomic) NSString *lastName;
#property (copy, nonatomic) NSString *dob;
#property (copy, nonatomic) NSString *gender;
#property (copy, nonatomic) NSString *avatarURL;
#property (assign, nonatomic) NSInteger status;
- (instancetype)initWithDictionary:(NSDictionary *)dictionary;
#end
And
// User.m
#implementation User
- (instancetype)initWithDictionary:(NSDictionary *)dictionary {
if ((self = [super init])) {
self.uid = dictionary[#"uid"];
self.firstName = dictionary[#"firstName"];
self.lastName = dictionary[#"lastName"];
self.dob = dictionary[#"dob"];
self.gender = dictionary[#"gender"];
self.status = [dictionary[#"status"] intValue];
self.avatarURL = dictionary[#"avatarURL"];
}
return self;
}
#end
And then, the caller would do:
User *user = [[User alloc] initWithDictionary:someDictionary];
There are other refinements you could consider here (e.g. readonly public interface, declaring nullability, lightweight generics on the dictionary, etc.), but the above is probably a good starting point.
By the way, if your wondering why I made these copy instead of strong, we just want to protect ourselves in case the caller passed a NSMutableString (which is a NSString subclass) and accidentally mutated it later. This is just a bit safer, a little more defensive pattern.

Saving a custom class in an array to be saved

I'm creating an app that will create a form which a user will then fill out and save for later use.
#interface DataModel : NSObject
#property (strong, nonatomic) NSString *whiskeyName;
#property (strong, nonatomic) NSNumber *whiskeyRating;
#property (strong, nonatomic) NSString *whiskeyColor;
#property (strong, nonatomic) NSString *whiskeyNose;
#property (strong, nonatomic) NSString *whiskeyFlavors;
#property (strong, nonatomic) NSString *whiskeyFinish;
#property (strong, nonatomic) NSString *whiskeyNotes;
The app will store multiple copies of these forms (think of Apple's Notes app). I've created a class composed of NSStrings and NSNumbers but I'm having a difficult time figuring out a way save them to an NSArray to access later. I've just started fooling around with Core Data but everything I've found will only save a single form. How can I save multiple versions of a class in an array to be opened and edited for later use? Sorry if the question is vague, but I've been hitting my head on a wall and am having a hard time finding a working solution.
If I understand correctly you have sub-classed NSObject and want to save multiple instances of this class into an array? If that is the case, then a MutableArray should be able to hold any object:
// .m file
#import "DataModel.h"
#interface YourViewController
#property (strong, nonatomic) NSMutableArray *myArray;
#end
#implementation YourViewController
- (void) viewDidLoad {
NSMutableArray *myArray = [[NSMutableArray alloc] initWithCapacity:numberOfObjectsToStore];
DataModel *myClassInstance1 = [[DataModel alloc] init];
myClassInstance1.whiskeyName= #"somevalue";
myClassInstance1.whiskeyRating= 5;
DataModel *myClassInstance2 = [[DataModel alloc] init];
myClassInstance2.whiskeyName= #"someothervalue";
myClassInstance2.whiskeyRating= 2;
[myArray addObject:myClassInstance1];
[myArray addObject:myClassInstance2];
}

NSCounted set ignoring a property [duplicate]

This question already has an answer here:
Confusion with distinct and indistinct object
(1 answer)
Closed 7 years ago.
I'm trying to do a NSCountedSet but I need to ignore a property, I tried overriding the isEqual method of my object, and doing a manual compare of all the properties except the one I want to ignore but it doesn't work
Any idea on how to achieve it?
UPDATE
My class
#interface GSOrderMenuMenuContent : GSBaseModel
#property (copy, nonatomic) NSNumber *order_content_id;
#property (copy, nonatomic) NSNumber *item_id;
#property (copy, nonatomic) NSNumber *price;
#property (copy, nonatomic) NSNumber *priceWithModifiers;
#property (copy, nonatomic) NSString *course;
#property (copy, nonatomic) NSString *itemName;
#property (nonatomic) GSMenuItem* item;
#property (nonatomic) NSMutableArray *modifiers;
#property (copy, nonatomic) NSNumber *isAlreadyPrinted;
#property (copy,nonatomic) NSNumber *isDeleted;
-(GSOrderMenuMenuContent*)deepCopy;
-(GSOrderContent*) orderContent;
-(BOOL)isEqual:(GSOrderMenuMenuContent*)object;
#end
Usage:
NSCountedSet* countedSet = [[NSCountedSet alloc] initWithArray:contents];
Where contents is an array of class objects
Adding a breakpoint to the isEqual gets not called (however it is if doing a [NSarray containsObject:...]
As mentioned in the docs for the isEqual: method, you must always implement a corresponding hash method. Those two methods must always be implemented together.
Two objects that compare as equal must also return the same hash value.

Add class to other as property

I want to add my custom NSObject class to other as property, is that possible?
Class look like this:
#interface PlaceHolder : NSObject
#property (strong, nonatomic) NSString *name;
#property (strong, nonatomic) NSString *description;
#property (strong, nonatomic) NSString *webPage;
I want to make it property of my other class to initialize it once, and then work with properties - name, description. webpage and other..
Do i need to create a category? Or there is another way to achieve this?
Any advice would be appreciated, thank you.
Just add a property to the class that needs to use it
#property (strong, nonatomic) PlaceHolder *placeHolder;

Xcode requiring me to redeclare properties as instance variables

I have an object called SCPFAd and it is declared in its header file as follows:
#interface SCPFAd : NSObject
#property (strong, nonatomic) NSArray *imageURLs;
#property (strong, nonatomic) NSString *title;
#property (strong, nonatomic) NSString *price;
#property (strong, nonatomic) NSString *longDescription;
#property (strong, nonatomic) SCPFLocation *location;
#property (strong, nonatomic) SCPFCategory *category;
#property (strong, nonatomic) NSArray *properties;
#property (readonly, strong, nonatomic) NSString *sellerID;
#property (readonly, strong, nonatomic) NSString *timePosted;
- (id)initWithRawData:(NSDictionary *)rawData;
- (BOOL)displaysPrice;
#end
In the implementation file, I have an SCPFAd extension declared this way:
#interface SCPFAd ()
{
NSMutableDictionary *_rawData;
NSMutableArray *_imageURLs;
NSString *_title;
NSString *_price;
NSString *_longDescription;
SCPFLocation *_location;
SCPFCategory *_category;
NSMutableArray *_properties;
}
#property (strong, nonatomic) NSDictionary *rawData;
#property (strong, nonatomic) NSString *sellerID;
#property (strong, nonatomic) NSString *timePosted;
#property (strong, nonatomic) NSString *adID;
#end
I deliberately redeclared the properties rawData, imageURLs, and properties as instance variables because I want external objects to access or assign them as immutable types, but I'll be changing them internally.
What I don't understand is why, when I override the setters, I get a compiler error that says it can't find the variables _title, _price, _longDescription, _location, and _category. The error goes away when I redeclare title, price, longDescription, location, and category as above, but I see it as unnecessary--nothing in the class extension changes their external declarations.
This is how I'm overriding setTitle, for example:
- (void)setTitle:(NSString *)title
{
_title = title;
_rawData[#"name"] = title;
}
- (NSString *)title
{
if (!_title) {
_title = _rawData[#"name"];
}
return _title;
}
If I comment out NSString *_title; in the extension, the compiler says it can't find _title in the first line of the setter, and wherever it occurs in the getter. The getter used to work just fine, though, even without the redeclaration.
If you declare a property and then override both the getter and setter, it won't auto-synthesize the property. But you can just add a line to synthesize it to your implementation:
#synthesize title = _title;
As for having a property be an immutable type, and its backing instance variable be mutable, you're going to have an issue when from outside your class the immutable type is assigned to it, and you treat it as the mutable version, because it won't respond to the methods to mutate it. For example, you assign an NSArray to a variable, then try to treat it as an NSMutableArray, it won't work.
If you implement a getter, the compiler doesn't automatically create an ivar.
This is for a good reason. The property may (and, in my experience, usually is) created on request and returned, so in that case no instance variable is needed to store it and it would add a significant memory overhead to classes with a large number of such properties if every getter had an associated ivar.
One other comment. This:
NSMutableDictionary *_rawData;
// ...
#property (strong, nonatomic) NSDictionary *rawData;
May cause you problems. If rawData is set with an immutable dictionary, it will raise an exception when you attempt to mutate it later. Make sure you copy it on assign using -mutableCopy. (I assume you aren't copying it because it's marked strong, not copy. If you are, it's fine)
When you override the setter and getter (not just the getter), Xcode assumes you want complete control and doesn't create the backing store (the _title). You have to do it yourself with
#synthesize title = _title
If you implement a getter and a setter for a read-write property, or a getter for a read-only property then Clang (Xcode) will not synthesise the backing instance variable - see Apple's Encapuslating Data, note in the section You Can Implement Custom Accessor Methods.
You are implementing both the setter and the getter so you must provide your own instance variable if needed.

Resources