How to assign NSArray data to NSMutableArray in iphone?
Asked Answered
V

3

14

In my application I have an NSArray which contains some data. I want to take that data and put it into an NSMutableArray called subArrayData. I am able to insert the data from my first array into the mutable array, but when the application runs I am getting:

warning:Incompatible pointer types assigning to 'nsmutablearray *' from 'nsarray *' please help out.

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]];


}
Vegetate answered 18/4, 2012 at 7:4 Comment(0)
T
23

change 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]];


}
Tress answered 18/4, 2012 at 7:12 Comment(1)
This has a very strange extra object in it--you can call initWithObjects: on NSMutableArray directly, bypassing the initWithArray:/arrayWithObjects: chain here. See Justin's answer.Vanhorn
E
25

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

NSArray *someArray = ...;
NSMutableArray* subArrayData = [someArray mutableCopy];
Eba answered 18/4, 2012 at 7:15 Comment(1)
This is the shortest way...Ewald
T
23

change 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]];


}
Tress answered 18/4, 2012 at 7:12 Comment(1)
This has a very strange extra object in it--you can call initWithObjects: on NSMutableArray directly, bypassing the initWithArray:/arrayWithObjects: chain here. See Justin's answer.Vanhorn
S
9

Just call one of NSMutableArray's initializers, like so:

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]];

For the cases when you are dealing with an existing NSArray, you can make a mutableCopy:

- (id)initWithArray:(NSArray *)array
{
  self = [super init];
  if (nil != self) {
    subArrayData = [array mutableCopy];
    ...
Stearic answered 18/4, 2012 at 7:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.