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

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.

Related

Accessing Arrays throughout classes xcode

In my xcode project, whenever I try using:
#import "Class1.h"
NSMutableArray *array;
#implementation Class2
Class1 *class = [Class1 new];
array = class.array;
Assuming there is something called array in Class1.
Xcode builds it like there are no errors. However, when I run it, the screen flashes what is on class1 before crashing and giving me an error.
I fixed some other code. Now the error is:
[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array
Sorry, I am new to Obj-C.
Designing your code. Design Pattern. These should be decided first, and thereafter coding is simpler.
As I can see you are creating a global array!!! Which is not good. If you want a shared array use SingletonClass/SharedClass.
Or you should put NSMutableArray *array; in .h file and alloc+init it to access it.
array = [class array];
This line is not clear, [class array] will return the array of Class1, so you must have NSArray in your Class2 where you are assiging Class1's array.
If Class2 has array as property use self.array=[class array];
If you want your array to be a property of Class1, you need to do something like this in your Class1.h file:
#interface Class1
#property (nonatomic, strong) NSMutableArray *array;
#end
Then you can access the array like in the code you posted.

Regarding NSPredicate and NSMutableArray deep copy

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;

initWithArray seems to give a reference to source array rather then a new array

In my code I use initWithArray like so :
vendorArray = [[NSMutableArray alloc] initWithArray:resultsCore.availableVendors];
But changes I make to the vendorArray also seem to be made to restultsCore.availableVendors.
This was not what I had desired.
I want populate vendorArray with restultsCore.availableVendors but then leave it untouched by the changes I make to vendorArray.
Is there away to get this functionality without using an for and if statements to copy the data object by object into vendorArray from restultsCore.availableVendors ?
Many Thanks
-Code
EDIT Below is my edit.
Vendors is an object i made
#interface Vendor : NSObject
{
NSString *vendorID;
NSMutableArray *availableCars;
BOOL atAirport;
}
#property (nonatomic, copy) NSString *vendorID;
#property (nonatomic, retain) NSMutableArray *availableCars;
#property (nonatomic, assign) BOOL atAirport;
- (id)initFromVehVendorAvailsDictionary:(NSDictionary *)vehVendorAvails;
#end
As you can see it has an array.
This array is another object I made called Car.
#interface Car : NSObject
{
NSMutableArray *vehicleCharges; // These are extras like breakdown assist etc
NSString *rateQualifier;
BOOL needCCInfo;
NSMutableArray *fees; // This array contains 3 dictionarys
NSMutableArray *pricedCoverages;
}
#property (nonatomic, retain) NSMutableArray *vehicleCharges;
#property (nonatomic, copy) NSString *rateQualifier;
#property (nonatomic, assign) BOOL needCCInfo;
#property (nonatomic, retain) NSMutableArray *fees;
#property (nonatomic, retain) NSMutableArray *pricedCoverages;
- (id) initFromVehicleDictionary:(NSDictionary *)vehicleDictionary;
#end
Car also has 3 arrays of my other objects. Its all very object oriented -.-
I've been reading up on NSCopying and saw a tutorial. But I still feel really lost as what to do here.
Do I need to implement the NSCopying protocol for my Vendors class, Car class and the other 3 classes that are contained in the arrays by car.
Vendors is the object contained in the array i want to copy. So is this the only class i need to implement the NSCopying tutorial for?
Many thanks, sorry for the long question
-Code
vendorArray = [[NSMutableArray alloc] initWithArray:resultsCore.availableVendors copyItems:YES];
Don't forget to implement the NSCopying protocol in the classes for the objects to be copied.

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

Accessing a global varibile in any class

Hey all i have been trying to figure out why i am getting this warning:
'TxtAppDelegate' may not respond to '-TCN'
'TxtAppDelegate' may not respond to '-TID'
when i try to use this code:
// .h file
#interface RootViewController : UITableViewController <UIActionSheetDelegate> {
NSString *theCompanyName;
NSString *theID;
}
#property (nonatomic, retain)NSString *theCompanyName;
#property (nonatomic, retain)NSString *theID;
// .m
NSString *theCompanyName;
NSString *theID;
#synthesize theCompanyName;
#synthesize theID;
TxtAppDelegate *customObjInstance = [[TxtAppDelegate alloc] init];
theCompanyName = [customObjInstance TCN];
theID = [customObjInstance TID];
I've added the header for the .h file that has the two functions in them. The code works but i really would like to solve the warning problem.
Any help would be great to solve this problem of mine :)
David
While it would have been more helpful to see the header file where TxtAppDelegate is declared, I'm guessing the method declarations must be off. They should look like this:
- (NSString *)TCN;
- (NSString *)TID;
If this is not the cause of the problem, please post the header file here so we can examine it.
How are declared these two functions in your header?
They should belong to a category of TxtAppDelegate class or to a protocol. If you choose the protocol, TxtAppDelegate interface should mention that it conforms to that protocol.