Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign NSArray data to NSMutableArray in iphone?

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


}
like image 469
Nilesh .S. Joshi Avatar asked Apr 18 '12 07:04

Nilesh .S. Joshi


People also ask

What's a difference between NSArray and NSSet?

The main difference is that NSArray is for an ordered collection and NSSet is for an unordered collection. There are several articles out there that talk about the difference in speed between the two, like this one. If you're iterating through an unordered collection, NSSet is great.

What is difference between array and NSArray in IOS?

Array is a struct, therefore it is a value type in Swift. NSArray is an immutable Objective C class, therefore it is a reference type in Swift and it is bridged to Array<AnyObject> . NSMutableArray is the mutable subclass of NSArray .

What is difference between NSArray and NSMutableArray?

The primary difference between NSArray and NSMutableArray is that a mutable array can be changed/modified after it has been allocated and initialized, whereas an immutable array, NSArray , cannot.

How do you declare NSArray in Objective-C?

Creating NSArray Objects Using Array Literals In addition to the provided initializers, such as initWithObjects: , you can create an NSArray object using an array literal. In Objective-C, the compiler generates code that makes an underlying call to the init(objects:count:) method.


2 Answers

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

NSArray *someArray = ...;
NSMutableArray* subArrayData = [someArray mutableCopy];
like image 140
Malhar Chudasama Avatar answered Sep 21 '22 16:09

Malhar Chudasama


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


}
like image 41
Manish Agrawal Avatar answered Sep 22 '22 16:09

Manish Agrawal