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]];
}
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.
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 .
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.
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.
You can convert any NSArray to an NSMutable array by calling its -mutableCopy method:
NSArray *someArray = ...;
NSMutableArray* subArrayData = [someArray mutableCopy];
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]];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With