Newbie to Objective-C....
I have a real simple .h file:
#interface IdentityManager : NSObject
#property (nonatomic, weak) NSString *username;
#property (nonatomic, weak) NSString *password;
#property (nonatomic, weak) NSString *connString;
#property (nonatomic, weak) NSString *token;
#end
And I want need to grab text from some text fields in another object to load into an Identity object:
self.identity.username = self.usernameTextField.text;
self.identity.password = self.passwordTextField.text;
Yep, it's a login page. Problem is that the username would not be set. After hours trying to find out why, I found that putting the value of self.usernameTextField.text into a local variable and passing the value of that to the Identity object worked:
NSString *tempUsername = self.usernameTextField.text;
self.identity.username = self.tempUsername;
self.identity.password = self.passwordTextField.text;
I have no idea why this would be. I can only guess that all my messing around has somehow left a trace of some old references in Xcode's cache somewhere.
More likely, I'm an idiot. Better, I'm still learning.
Should I be using NSMutableString?
I think something similar is happening again elsewhere. Use of a temp variable helping to achieve my goal.
Any thoughts anyone?
I don't really think your second solution actually fixes the issue. It's probably something else.
Though it's really wrong for a manager class to have a weak reference. In OOP, classes and especially managers should own the object they have as property.
So you should use strong references instead of weak:
#property (nonatomic, strong) NSString *username;
Additionally, you don't want any changes outside the class to modify the variable, so you should be passing a copy of the object:
NSString *username = self.usernameTextField.text;
self.identity.username = [username copy];
Alternatively, you can declare the property as copy instead of strong and you don't have to worry about copying the string every time you set it. (Credit to albertamg)
#property (nonatomic, copy) NSString *username;
Related
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.
Im trying to pass an array of 'Employee' objects iPhone to Apple Watch by serializing the array :
NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:employees];
and unserializing it as on the Watch side:
NSMutableArray *employees = [NSKeyedUnarchiver unarchiveObjectWithData:encodedObject];
This is the 'Employee' class:
#interface Employee : NSManagedObject
#property (nonatomic, retain) NSNumber * employeeID;
#property (nonatomic, retain) NSString * name;
#property (nonatomic, retain) NSNumber * age;
#property (nonatomic, retain) NSString * address;
#property (nonatomic, retain) NSString * designation;
#property (nonatomic, retain) NSString * teamName;
#property (nonatomic, retain) NSString * gender;
#property (nonatomic, retain) NSNumber * dateOfJoining;
#end
Do I have to do any changes on the Watch side to fix this error?
so I just had that exact same problem and the answer is simple but a little hard to find by oneself.
You simply have to use:
NSKeyedArchiver.setClassName("Employee", for: Employee.self)
before serializing
NSKeyedUnarchiver.setClass(Employee.self, forClassName: "Employee")
before deserializing
wherever needed.
Looks like iOS extensions prefix the class name with the extension's name.
For me it was happening in my Today extension. What fixed it was adding #objc(MyExampleClass) before the declaration.
#objc(MyExampleClass)
open class MyExampleClass {
....
}
teriiehina's answer got me part of the way there; I could archive and unarchive to clean devices but still got the above error when trying to unarchive an existing archive.
Eventually I found this question:
Added a custom framework, now Swift can't unarchive data, which the user answered himself:
Moving DemoNote from the app to a framework did change the module name, which meant that NSKeyedUnarchiver couldn't find instances of the archived class due to a name mismatch.
His solution of prefixing the old project's name to the className string (e.g. if the project was called "CompanyDirectory" then using "CompanyDirectory.Employee" as opposed to just "Employee") was what I needed to be able to unarchive my data from my model which had been moved into a newly-created linked Framework.
My understanding so far is that (retain) increases the reference count of a property and is essentially the exact same as (strong). Since all properties are set to retain by default (unless specified otherwise), is adding (strong) needed at all:
#property(nonatomic, strong) NSString *name;
Is the same as:
#property(nonatomic) NSString *name;
Both the above are the same, right?
Since ARC was introduced, "strong", "atomic", and "readwrite" are set by default.
These properties are equivalent:
#property NSArray *name;
#property (strong, atomic, readwrite) NSArray *name;
Source: http://useyourloaf.com/blog/default-property-attributes-with-arc.html
From the documentation:
By default, both Objective-C properties and variables maintain strong
references to their objects.
So both forms are the same.
I have a custom NSObject called MAAssignment. It's basically a data type that has a number of #properties and one custom init method:
#property (nonatomic, strong) NSDate *date;
#property (nonatomic, strong) NSString *assignmentName;
#property (nonatomic, strong) NSNumber *totalPoints;
#property (nonatomic, strong) NSNumber *recievedPoints;
#property (nonatomic, strong) NSNumber *classAverage;
#property (nonatomic, strong) NSNumber *extraCredit;
#property (nonatomic, strong) NSNumber *notGraded;
- (id)initWithDate:(NSString *)date assignmentName:(NSString *)assignmentName totalPoints:(NSNumber *)totalPoints recievedPoints:(NSNumber *)recievedPoints classAverage:(NSString *)classAverage extraCredit:(NSNumber *)extraCredit notGraded:(NSNumber *)notGraded;
I create an instance of it in the viewController, hoping to populate the newly created item with some data... But I can't figure out how to access the variables of the object. I went MAAssignment *assignment = [[MAAssignment alloc] init];, then I tried [assignment setDate] or assignment.date = ddate but none of them seem to work.
Am I misunderstanding how the accessors for objects work?
You should place these variables in h. file
of MAAsignment
I'm not understanding your question very well (where are you declaring this code? What's the full code of your MAAsiignment initialization example?), however I'll try to answer anyway.
Are you sure that the #property declarations are inside the #interface in .h and not inside .m?
The #interface inside .m is a private class extensions, and allows you to declare private properties. To make them available outside, you need to put them inside the header (.h).
Please post more code to let us provide a more exhaustive answer.
What you describe is a data container object. It should work as described:
MAAssignment *anAssignment = [[MAAssignment alloc] init];
anAssignment.date = [NSDate date];
anAssignment.totalPoints = #(10);
NSLog(#"anAssignment.date = %#", anAssignment.date);
NSLog(#"anAssignment.totalPoints = %#", anAssignment.totalPoints);
Should work perfectly. Are you getting any warnings?
I have two core data models with int64_t properties. One of them works fine while the other throws EXC_BAD_ACCESS when I try to assign a non-zero value to the integer field. I've read the answers that say to recreate the NSManagedObject child class and I have done with no success. The broken class looks like this:
#interface NoteObject : NSManagedObject
#property (nonatomic) int64_t remoteID;
#property (nonatomic) int64_t remoteArticleID;
#property (strong, nonatomic) ArticleObject *article;
#property (strong, nonatomic) NSString *status;
#property (strong, nonatomic) NSString *token;
#property (strong, nonatomic) NSString *title;
#property (strong, nonatomic) NSString *noteContent;
#property (strong, nonatomic) NSDate *pubDate;
#property (strong, nonatomic) NSDate *modDate;
#end
#implementation NoteObject
#dynamic remoteID;
#dynamic remoteArticleID;
#dynamic article;
#dynamic status;
#dynamic token;
#dynamic title;
#dynamic noteContent;
#dynamic pubDate;
#dynamic modDate;
#end
The offending line is in this block:
_noteObject = [NSEntityDescription insertNewObjectForEntityForName:#"Note" inManagedObjectContext:self.managedObjectContext];
_noteObject.remoteArticleID = 0; // this works
_noteObject.remoteArticleID = 1; // this crashes
What really has me stumped is that in another model I have the same fields with the same types and they will accept non-zero values without any trouble:
bookmarkObject = [NSEntityDescription insertNewObjectForEntityForName:#"Bookmark" inManagedObjectContext:self.managedObjectContext];
bookmarkObject.remoteArticleID = 0; // this works
bookmarkObject.remoteArticleID = 1; // this works, too
Is there anything in my .xcdatamodeld file that could be causing this?
EDIT
My data models look like this:
I had exactly the same problem.
It appears that xcode (or perhaps the compiler, or perhaps the two between them) sometimes gets confused when you manually edit properties in the NSManagedObject - it ends up treating our integers as pointers and trying to access memory directly - hence the EXC_BAD_ACCESS.
Anyway, as this question explains: SO Question, the solution is to delete your old class (obviously copy out any custom code so you can paste it back again later) and then get xcode to regenerate it for you (select the entity in the data model and select "Editor / Create NSManagedObject subclass..."). In the dialogue that appears, make sure "Use scalar properties for primitive data types" is ticked.
You may have to manually edit the resulting class to turn some non scalar properties back into objects (I had a date object which it turned into something other than NSDate - I forget exactly what, but it accepted the manually made edit back to NSDate).
It worked for me. Hope it works for you.
Ali
Well, in case anyone else is having this issue, I never found a satisfactory answer for why one entity was working and the other wasn't. My workaround was to refactor the properties to use NSNumber wrappers instead of primitive int64_t values.
#property (strong, nonatomic) NSNumber *remoteID;
#property (strong, nonatomic) NSNumber *remoteArticleID;
Of course, that means boxing/unboxing the integer values.
_noteObject.remoteArticleID = [NSNumber numberWithInt:1];
int intVar = [_noteObject.remoteArticleID intValue];
In your model file, check that the entity's "Class" property is set to the appropriate class, and not the default NSManagedObject.
If you leave it as NSManagedObject, Core Data will create properties itself on a custom NSManagedObject subclass it generates itself, rather than using your own subclass. Most getters and setters will appear to work, but you may have issues with non-boxed primitive properties and custom getters and setters.