Regarding NSPredicate and NSMutableArray deep copy - iphone

I am trying to copy NSMutableArray which is based of my Registration NSMutableArray Class and trying to filter a boolean. The concern is, since the nsmutablearray is from the class, each time I try to alloc and initwitharray:[self person] man]; where man is a nsmutablearray it doesn't allow me to do it. Is this functionality only localized or can be globally utilized as well? Or am I missing something here?
Thanks.

You have to make sure that "man" is declared as a property of "[self person]". i.e. in the header of the class that [self person] is an instance of,
#property (nonatomic, retain) NSMutableArray *man;
and in the implementation:
#synthesize man;

Related

iOS error on array initialisation

I'm trying to initialise an array of Photos, and am doing so like this:
NSMutableArray *photoList = [[NSMutableArray alloc] init];
self.photos = photoList;
But when this line of code runs, I get this error:
'NSInvalidArgumentException', reason: '-[Project setPhotos:]: unrecognized selector sent to instance 0xee8e310'
I've spent about three hours trying to find a fix but couldn't, can anybody tell me what to do?
It seems to me that you haven't created a property photos and if you have then it would also seem that this property is not #synthesize'd in your implementation, maybe you are using #dynamic, in which case it is up to you to create a - (void)setPhotos:(NSMutableArray*)photos; method
self does not have a property called photos
You need to add #property (nonatomic, strong) NSArray *photos; in your .h file, before the #end
and in the .m file, #synthesize photo;
This line of code:
self.photos = photoList;
gets turned into this line
[self setPhotos:photoList];
by the compiler - the dot notation is what is called "syntatic sugar" as is just makes it easier to type, it doesn't really shorten the code.
If you have created your own getters and setters (ie)
- (NSMutableArray *)photos;
- (void)setPhotos:(NSMutableArray *)myPhotos
Then you can use that sugar even though you don't have a property called "photos" - although this is considered a misuse of the feature (I show it for comparison purposes).
When you create a property named photos:
#property (nonatomic, strong) NSMutableArray *photos;
the compiler will generate an ivar for you using the same name, but not create the getters and setters. The line:
#synthesize photos;
asks the compiler to do a getter (in all cases) and a setter (if the property is read write). If you do not provide a #synthesize statement, the compiler normally complains, so people should be observing these warnings.
You can see in the error that you have no setPhotos, thus your problem can be fixed quite easily.
Seems like you haven't written a setter method for photos.
in your .h
#property (strong, nonatomic) NSArray * photos;
in your .m (if not using xcode 4.4)
#synthesize photos;
or you could just try writing it like this
photos = [NSArray arrayWithArray:photoList];
For reference if you use [self setMyArray:array] or self.array = myArray
Then you are using the setter method, which is something you probably want to do. If you just point array = myArray, you would be pointing to myArray and if it were released your pointer would point in to the abyss. It's good to not to do that, not using the setter means you are accessing the variable photos directly and this may not be what you want.
Whoops, I'd used #dynamic photos instead of #synthesize photos

How do I populate an NSMutableArray in one class with another object?

I know this is a simple answer, but I can't seem to find the solution. I created an object in its own class and I am trying to populate it with data from another class. For simple data types like NSString, I have no problem, but when trying make an NSMutableArray equal to another NSMutableArray or when I try to populate a NSMutableArray with another objects (like strings), I keep getting exception errors...
Here is the object I am trying to populate:
#import <Foundation/Foundation.h>
#interface RSSFeedList : NSObject {
NSString *subject;
NSMutableArray *rssfeedDetail;
}
#property (nonatomic, retain) NSString *subject;
#property (nonatomic, retain) NSMutableArray *rssfeedDetail;
#end
This is how I was able to populate the NSString 'subject' in another class:
rssFeedList.subject = #"test";
However, if I follow similar convention within that same class with respect to an Array, it throws an exception:
rssFeedList.rssfeedDetail = rssItemDetailArray;
Where rssItemDetailArray is a NSMutableArray that I have built in the same class.
I have also tried to add items (i tried strings for testing) to the NSMutableArray directly like so to no avail:
[rssFeedList.rssfeedDetail addObject:#"test"];
Any ideas?? Thanks in advance!!
I'm almost certain that you've forgotten to synthesize rssfeedDetail.

Memory management in iphone

I have a class where i have define a array as a retain property such as:
#property (nonatomic,retain) NSMutableArray *temp;
can i use the self.temp = self.temp;
if yes can any one tell setter method for this and step by step implementation for this
looking forward from here.
Thanks
If you want a setter method declare your NSMutableArray in your header file:
{
NSMutableArray *temp;
}
#property(nonatomic, retain) NSMutableArray *temp;
And then in the main file make sure you call this after the #implementation MyClass:
#synthesize temp;
This gives you your getter and setter methods so from outside this class you can call MyClass.temp = ...;

NSMutableString appendString generates a SIGABRT

This makes no sense to me. Maybe someone here can explain why this happens.
I've got an NSMutableString that I alloc at the top of my iPhone app, then append to later in the process. It results in a SIGABRT, which doesn't add up to me. Here's the code:
Header File (simplified):
#interface MyAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
NSMutableString *locationErrorMessage;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, copy) NSMutableString *locationErrorMessage;
#end
And the relevant parts of the Main:
#implementation MyAppDelegate
#synthesize window;
#synthesize locationErrorMessage;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
self.locationErrorMessage = [[NSMutableString alloc] init];
}
- (void)anotherFunction {
[self.locationErrorMessage appendString: #"Blah Blah Blah"];
}
This all seems simple enough. What am I missing?
I would call this a bug in how property setters are generated, but the answer is pretty simple:
You declared the property as (nonatomic, copy). This means that whenever the locationErrorMessage property is set, it's going to invoke copy on the new value and use that copy as the property value.
Unfortunately, invoking copy on an NSMutableString does not result in an NSMutableString, it results in an NSString (which cannot be mutated using something like appendString:).
So the simple fix would be to change the property declaration from copy to retain.
(I would say that the bug would be: If you declare a property for a mutable object as copy, then the copy setter should actually use mutableCopy and not copy) => rdar://8416047
Your property is copying the passed in string. A copy always is immutable, so you’re trying to send appendString: to an immutable NSString. Declare your property as retain and it will work or write a custom setter that copies the string using mutableCopy.
You also have a memory leak, you should use [NSMutableString string] instead of the alloc-init sequence.
Btw, you have a leak there,
self.locationErrorMessage = [[NSMutableString alloc] init];
you're copying the value, but you never release the actual first allocated NSMutableString.

Access a NSMutableArray of an object

I post this topic because I have a problem with my iPhone application since 3 days. I hope someone can help me because I'm going crazy.
Here is the thing : I fill in an object userXMLData,in the delegate of my application, with a XML Parser. This object contains many NSStrings and a NSMutableArrays which contains objects type Album to.
My problem is : I can display all data of userXMLData with an internal function, but when I'm trying to get the data from the array in my viewController , it doesn't work. I mean, it crashes. It's weird because I can access to the appDelegate.userXMLData.NSString but not of my appDelegate.userXMLData.NSMutableArray
Here is my code :
// Initializaiton in the delegate
userXMLData = [[UserXMLData alloc] init];
userXMLData.myArray = [[NSMutableArray alloc] init];
UserXMLData.h
#interface UserXMLData : NSObject {
// User Data
NSString *userId;
// Content
NSMutableArray *myArray;
}
#property(nonatomic, retain) NSString *myString;
#property(nonatomic, copy) NSMutableArray *myArray;
#end
//Album.h
#interface Album : NSObject {
NSString *albumId;
NSMutableArray *content;
}
#property(nonatomic, retain) NSString *albumId;
#property(nonatomic, retain) NSMutableArray *content;
#end
As I said, I really don't why it crashes. I'm stuck and I cannot continue my application without fixing it.
Enable Zombies by following the instructions here:
http://loufranco.com/blog/files/debugging-memory-iphone.html
This will cause your app to not release any objects and instead cause them to complain to the console if messages are sent to them after they are released.
The most common cause of a crash is releasing too often (or retaining too few times).
Also, running a Build and Analyze can sometimes point these out.
Would be able to answer better if you'd show the code where you are trying to access the array and the error you receive on crash, but I'd hazard a guess that you don't have #synthesize myArray in your implementation (.m) file