My project is ARC-enabled, I have two models something like:
Category:
#interface Parent : NSObject
#property (assign, nonatomic) Category *parent;
#property (strong, nonatomic) NSArray *children;
#property (strong, nonatomic) NSArray *subCategories;
#end
Product:
#interface Child : NSObject
#property (strong, nonatomic) NSString *name;
#property (assign, nonatomic) Category *category;
#end
To avoid reference loop, and to deploy on iOS 4.x, I set the parent property of Child as assign but not weak.
From JSON data, I can get a tree containing my categories and products. When I use the tree to enumerate, all the things went perfect. And I have a recursion method to search products with name matching a keyword. The method is something as followed:
- (NSArray *)productsWithKeyword:(NSString *)keyword
{
NSMutableArray *filteredProducts = [NSMutableArray array];
for (Category *cat in self.children) {
NSArray *subResult = [cat productsWithKeyword:keyword];
[filteredProducts addObjectsFromArray:subResult];
}
return [filteredProducts copy];
}
The problem is when I'm using the returned array, I can get the right category property. But from the second time and on, the category property becomes 0 (caught from debugger).
So, what's the problem? And what's the fix to the problem?
To avoid a reference loop you want to make one object have a weak reference to the other not have both with weak references to each other. I think what's happening in this case is that the product doesn't own the category and the category doesn't own the product, so ARC releases both of the objects assuming no one claims them.
FIX: make one of these objects the "parent" of the other.
Related
I have an object with a property of NSArray that contains other NSArrays of NSNumbers. I've added a lightweight generic to the property definition in the header file like so:
#property (strong, nonatomic, readonly) NSArray<NSArray *> *myArray;
The generated Swift interface shows:
public var myArray: [[AnyObject]] { get }
Is there a way that I can further mark up my property declaration to indicate that this is actually [[NSNumber]]? The compiler gets upset with me when I try NSArray<NSArray *<NSNumber *>> *myArray; or NSArray<NSArray *><NSNumber *> *myArray;.
#property (strong, nonatomic, readonly) NSArray<NSArray<NSNumber *> *> *myArray;
All the stars have move to the left.
I've read the other threads about arrays of UIImageView-objects, but I can't get it to work. Note that I'm completely new to Objective-c.
I have some UIImageView objects declared in the .h - file like this:
#property (weak, nonatomic) IBOutlet UIImageView *obj1;
I also have an array declared at the same place:
#property (weak, nonatomic) IBOutlet NSArray *arr;
In the viewDidLoad-method I want to add obj1 to arr. How can I do that?
I've tested to initialize arr and add objects to it, but it doesn't work.
_arr = [[NSMutableArray alloc] init];
[_arr addObjects: obj1];
Hank
make sure in debugger that _arr is not nil. You need to declare it as
#property (strong, nonatomic) NSMutableArray *arr;
since arr is not a ui control on view controller, you do not need to add IBOutlet.
Also, your view controller posses it, so it should be strong rather than weak.
You also should use NSMutableArray instead of NSArray, since only the first one can be used for adding objects, and this is how you say to the compiler of the type of this object
Change
#property (weak, nonatomic) IBOutlet NSArray *arr;
to
#property (strong, nonatomic) NSMutableArray *arr;
If you want to add UIImageView Object in your Array you can do that.But the thing is if your want to add object in NSArrar after its initialization than you have to declare NSMutableArray as you can add and remove object in that than the addObject method would work.
Also their is no need to declare NSArray of NSMutableArray as IBOutlet.
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.
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.
I'm trying to make a deep copy of an NSMutableArray whose objects are instances of a custom class similar to this:
#interface CustomParent : NSObject
#property NSInteger Id;
#property (strong, nonatomic) NSString *IdStr;
#property (weak, nonatomic) NSDate *Date;
#property (strong, nonatomic) NSMutableArray *CustomChildren;
#property (strong, nonatomic) CustomType *Type;
#property float Value;
#end
I know there are lots of posts dealing with copying objects, but I donĀ“t find examples for getting a complete copy of objects with collection members or properties. NSMutableArray *dstArray = [[NSMutableArray alloc] initWithArray:srcArray copyItems:YES]; raises an exception involving the copyWithZone method.
How can I do this? Thanks!
In order to deep copy the content of the array
[[NSMutableArray alloc] initWithArray:srcArray copyItems:YES];
will send copyWithZone: to every object inside the collection. If they don't respond to this selector, you'll get a crash.
Have your CustomParent class to conform to the NSCopying protocol and you're done.
Here's some extra info on how do achieve it: Implementing NSCopying