How to assign NSArray data to NSMutableArray in iphone?

In my application, I have an NSArray that contains some data. I want to take this data and put it in an NSMutableArray called subArrayData . I can insert data from my first array into a mutable array, but when the application starts, I get:

warning: Incompatible pointer types assigned by 'nsmutablearray *' from 'nsarray *', please help.

following is my code: .h file

 #import <UIKit/UIKit.h> @interface AddNew : UIViewController { NSMutableArray *subArrayData;; } @property(nonatomic ,retain)NSMutableArray *subArrayData; 

.m file

 #import "AddNew.h" #import "DashBoardPage.h" #import "SubmitYourListing.h" @implementation AddNew @synthesize subArrayData; -(void)accommodationAndTravel { subArrayData =[[NSArray alloc] initWithArray:[NSArray arrayWithObjects:@"Select one",@"Accommodation and travel hospitality",@"Apartments and villas",@"Bed and Breakfast",@"Caravan parks and campsites",@"Hospitality", @"Hotels and Motels",@"Snow and Ski lodges",@"Tourist attractions and tourism information",@"Tours and Holidays",@"Travel agents and Services",nil]]; } 
+8
object ios objective-c iphone
source share
3 answers

modify the .m file

 #import "AddNew.h" #import "DashBoardPage.h" #import "SubmitYourListing.h" @implementation AddNew @synthesize subArrayData; -(void)accommodationAndTravel { subArrayData =[[NSMutableArray alloc] initWithArray:[NSArray arrayWithObjects:@"Select one",@"Accommodation and travel hospitality",@"Apartments and villas",@"Bed and Breakfast",@"Caravan parks and campsites",@"Hospitality", @"Hotels and Motels",@"Snow and Ski lodges",@"Tourist attractions and tourism information",@"Tours and Holidays",@"Travel agents and Services",nil]]; } 
+17
source share

You can convert any NSArray to an NSMutable array by calling its -mutableCopy method:

 NSArray *someArray = ...; NSMutableArray* subArrayData = [someArray mutableCopy]; 
+18
source share

Just call one of the NSMutableArray initializers, for example:

 subArrayData = [[NSMutableArray alloc] initWithObjects: @"Select one", @"Accommodation and travel hospitality", @"Apartments and villas", @"Bed and Breakfast", @"Caravan parks and campsites", @"Hospitality", @"Hotels and Motels", @"Snow and Ski lodges", @"Tourist attractions and tourism information", @"Tours and Holidays", @"Travel agents and Services", nil]]; 

In cases where you are dealing with an existing NSArray , you can do mutableCopy :

 - (id)initWithArray:(NSArray *)array { self = [super init]; if (nil != self) { subArrayData = [array mutableCopy]; ... 
+9
source share

All Articles