'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil' - ios

I have an array with the names of the images in the form of strings, I want to transform it into an array of images and I getting this error, what I did wrong?
'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
My code:
NSString * immagini = self.chinaTable.images; //unique string
NSArray * arrayImages = [immagini componentsSeparatedByString:#";"];
NSLog(#"The images: %#", arrayImages);// here are strings
/*The images: (
"ArchUrb_PortaGenova1.jpg",
"ArchUrb_PortaGenova2.jpg",
"ArchUrb_PortaGenova3.jpg",
"ArchUrb_PortaGenova4.jpg"
)*/
NSMutableArray * mutableImages =[[NSMutableArray alloc]initWithCapacity:20];
for (id obj in arrayImages){
/*The images: (
"ArchUrb_PortaGenova1.jpg",
"ArchUrb_PortaGenova2.jpg",
"ArchUrb_PortaGenova3.jpg",
"ArchUrb_PortaGenova4.jpg"
)*/
[mutableImages addObject:[UIImage imageNamed:obj]];//here comes the error
NSLog(#"The array mutable is è %#", mutableImages);
}
NSLog(#"The array of images %#", mutableImages);
self.viewImages.animationImages = [NSArray arrayWithArray:mutableImages];
self.viewImages.animationDuration =3;
self.viewImages.animationRepeatCount= 0;
[self.viewImages startAnimating];

[UIImage imageNamed:] returns nil if it can't find the requested image in your app bundle.
Add missing image to your project, or add an if before adding image to array, like this:
for (id obj in arrayImages){
UIImage *image = [UIImage imageNamed:obj];
if (image != nil)
{
[mutableImages addObject:image];
}
NSLog(#"The array mutable is è %#", mutableImages);
}

Change your code segment as follows
for (id obj in arrayImages){
UIImage *image = [UIImage imageNamed:obj];
if ( image ) {
[mutableImages addObject:image];
NSLog(#"The array mutable is è %#", mutableImages);
}
}

This question is related to the question: Exception with insertObject:atIndex: on iOS6. So please read it to find your solution.

Related

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString isFileURL]: unrecognized selector sent to instance

I have an array of url:
imageurl =
(
"http://10.1.1.4:8084/Photos/AA/c6aee8617ec94116911e17f745ced4d8.jpg",
"http://10.1.1.4:8084/Photos/AA/75764b74fbc440c790ff235e5336223e.jpg",
"http://10.1.1.4:8084/Photos/AA/4b390e733d8c48a6931079120af60b0a.jpg",
"http://10.1.1.4:8084/Photos/AA/2fade8440ae74b4dabdaff5dc13c5128.jpg"
);
I am trying to implement horizontal scroll view with images on url using the following code:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
int pageCount=4;
_scroller.pagingEnabled=YES;
_scroller.contentSize=CGSizeMake(pageCount*_scroller.bounds.size.width,_scroller.bounds.size.height);
CGRect ViewSize=_scroller.bounds;
NSArray *imgArray = [self.tripDetails valueForKey:#"Flightimageurl"];
for(int i=0;i<[imgArray count];i++)
{
UIImageView *imgView1=[[UIImageView alloc]initWithFrame:ViewSize];
NSURL *url=[imgArray objectAtIndex:i];
NSData *data = [NSData dataWithContentsOfURL:url];
imgView1.image=[UIImage imageWithData:data];
[_scroller addSubview:imgView1];
[self.view addSubview:_scroller];
ViewSize =CGRectOffset(ViewSize,_scroller.bounds.size.width,0);
}
But this crashes and give the above exception. How this can be fixed and image from url can be shown on imageView.
You are getting this crash because your imgArray contains NSString objects not NSURL objects, so first you need to create NSURL instance from that NSString instance.
NSURL *url = [NSURL URLWithString:[imgArray objectAtIndex:i]];

NSArrayM unrecognized selector error

I need some help understanding why I am getting an unrecognized selector error
I have a NSMutableDictionary
#interface CallDetailViewController () {
NSMutableDictionary * thisDictionary;
}
#end
That gets populated from the MasterTabController
- (void) some method {
thisDictionary = [[(MasterTabController *)self.tabBarController detailDictionary] mutableCopy];
NSLog(#"Check dictinoary: %#", [thisDictionary description]);
}
Outputting a description shows the data is there
Check dictinoary: (
{
DATEIN = "2015-12-11 13:33:41";
"ETA" = "2015-12-14 13:54:16";
RecordKey = 2961;
Destination = "Some address";
Location = "Some location";
} )
But when I try to access an object in it:
NSLog(#"Destination: %#", [thisDictionary objectForKey:#"Destination"]);
I get the following error.
-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x7a73f9a0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKey:]:
unrecognized selector sent to instance 0x7a73f9a0'
The array is populated from a JSON value that returns an array of records called Record. After the JSON is returned, I pull out just section that contains the record.
NSDictionary * dictTowx = [NSDictionary dictionaryWithDictionary:[dictJSON objectForKey:#"ThisResponse"]];
NSDictionary * dictData = [NSDictionary dictionaryWithDictionary:[dictTowx objectForKey:#"Data"]];
NSArray * arrRecordSet = [dictData objectForKey:#"Recordset"];
NSDictionary * dicRecord= [arrRecordSet objectAtIndex:0];
self.detailDictionary = [dicRecord objectForKey:#"Record"];
The RAW JSON:
{
ThisResponse = {
Data = {
Recordset = (
{
Record = (
{
DATEIN = "2015-12-11 13:33:41";
"ETA" = "2015-12-14 13:54:16";
RecordKey = 2961;
Destination = "Some address";
Location = "Some location";
}
);
}
);
};
};
}
What am I doing wrong?
Please try to assign like this
self.detailDictionary = [[dicRecord objectForKey:#"Record"] objectAtIndex:0];
and then access like this
NSLog(#"Destination: %#", [thisDictionary objectForKey:#"Destination"]);
Enjoy programming.
- (void) some method {
thisDictionary = [[(MasterTabController *)self.tabBarController detailDictionary] mutableCopy];
NSLog(#"Check dictinoary: %#", [thisDictionary description]);
}
try replacing
thisDictionary = [[(MasterTabController *)self.tabBarController detailDictionary] mutableCopy];
with
thisDictionary = [[NSMutableDictionary alloc]initWithDictionary: [[{MasterTabController *)self.tabBarController detailDictionary] mutableCopy]]];
i think your problem may be you are not instantiating the dictionary before setting it to objects from another.

Terminating app due to uncaught exception 'NSRangeException', reason

I am using search bar in collection,when I am doing filter text I got the error like
` Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2147483647 beyond bounds [0 .. 37]'
*** First throw call stack:
`
Here is my code
- (void)filterListForSearchText:(NSString *)searchText
{
for (NSString *title in _arrayCCName) {
NSRange nameRange = [title rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (nameRange.location != NSNotFound) {
[_searchResultName addObject:title];
}
}
for (int i=0; i<_searchResultName.count; i++) {
NSString *str=[ObjCls objectAtIndex:i];
NSInteger index=[_searchResultName indexOfObject:str];
[_searchResultDeignation addObject:[_arrayCCDesignation objectAtIndex:index]];
[_searchResultProfilePicture addObject:[_arrayCCProfilePicture objectAtIndex:index]];
[_searchResultFamilyPicture addObject:[_arrayCCFamilyPicture objectAtIndex:index]];
NSLog(#"array index is %ld",(long)index);
}
}
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
SEARCHBAR.showsCancelButton=NO;
[self.view endEditing:YES];
}
I got above error please help me how it is solve?
Simply means that the value of index is greater than the size of array at some point. When using [array objecAtIndex:index], index must be less than array.count.
These lines here....
NSInteger index=[_searchResultName indexOfObject:str];
[_searchResultDeignation addObject:[_arrayCCDesignation objectAtIndex:index]];
[_searchResultProfilePicture addObject:[_arrayCCProfilePicture objectAtIndex:index]];
[_searchResultFamilyPicture addObject:[_arrayCCFamilyPicture objectAtIndex:index]];
NSLog(#"array index is %ld",(long)index);
You are assuming that the indexOfObject actually finds something. What happens if the str doesn't exist
Taken from the documentation....
Declaration
OBJECTIVE-C
- (NSUInteger)indexOfObject:(id)anObject
Parameters
anObject
An object.
Return Value
The lowest index whose corresponding array value is equal to anObject. If none of the objects in the array is equal to anObject, returns NSNotFound.

Get Row from NSArray

Hello i get a json that looks like this:
features: (
{
attributes = {
Gecontroleerd = Ja;
};
geometry = {
x = "5.968097965285907";
y = "52.50707112779077";
};
}
)
From this code:
NSDictionary *root = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
NSArray *data = [root objectForKey:#"features"];
NSLog(#"features: %#", data );
for (NSArray *row in data) {
NSString *latitude = row[5];
NSString *longitude = row[7];
NSString *crimeDescription = #"test";
NSString *address = #"banaan";
And u need to x values 5.968097965285907 for latitude
and y values 52.50707112779077 for longitude
But i get this error:
[__NSCFDictionary objectAtIndexedSubscript:]: unrecognized selector sent to instance 0x14831450
2012-11-14 10:10:59.000 ArrestPlotter[6330:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndexedSubscript:]: unrecognized selector sent to instance 0x14831450'
*** First throw call stack:
(0x1860012 0x1655e7e 0x18eb4bd 0x184fbbc 0x184f94e 0x2dbc 0x3b8f 0x1cc1e 0x16696b0 0xfa0035 0x17e3f3f 0x17e396f 0x1806734 0x1805f44 0x1805e1b 0x224a7e3 0x224a668 0x4a765c 0x25bd 0x24e5 0x1)
libc++abi.dylib: terminate called throwing an exception
(lldb)
Does anyone wich row i need to select?
I guess that the only thing is that the row number needs to be changed. Or maybe there should be something like this : [1][5]. Im not quite sure how this works
NSArray *data = [root objectForKey:#"features"];
NSLog(#"features: %#", data );
for (NSDictionary *dic in data) {
NSDictionary geometry = [dic objectForKey:#"geometry"];
// Do what you want..
NSString *myAwesomeX = [geometry objectForKey:#"x"];
NSString *myAwesomeY = [geometry objectForKey:#"y"];
}
The problem here is that you are trying to send a selector message to object row, that is in memory a NSDictionary (NSCFDictionary?) object, and you are trying to manage it like a NSArray.
The method objectAtIndexedSubscript (is underlying called by row[5] and row[7]) exists in NSDictionary, but no in NSArray.
Change
for (NSArray *row in data) {
by
for (NSDictionary *row in data) {
Also, you have to change the management of data inside for, look at the result of your log statement and act accord whit it.
I hope this will help!

objectForKey crash? iOS

I am trying to use the following code to load a UIButton and UITextField with information based upon a UISegmentedControl's current segment that is clicked but it is causing a SIGABRT crash.
Here is the code:
- (void)updateInfo {
NSLog(#"count1:%d", [self.accounts count]);
[self saveInfo];
NSLog(#"count2:%d", [self.accounts count]);
NSDictionary *dict = [self.accounts objectAtIndex:0];
NSData *imageData = [dict objectForKey:#"ProfileImage"];
UIImage *imageProfile = [UIImage imageWithData:imageData];
[image1 setImage:imageProfile];
NSDictionary *dict2 = [self.accounts objectAtIndex:1];
NSData *imageData2 = [dict2 objectForKey:#"ProfileImage"];
UIImage *imageProfile2 = [UIImage imageWithData:imageData2];
[image2 setImage:imageProfile2];
if ([self.accounts objectAtIndex:accountSC.selectedSegmentIndex] != nil) {
NSDictionary *dict = [self.accounts objectAtIndex:accountSC.selectedSegmentIndex];
//NSString *name = [dict objectForKey:#"Name"];
NSString *name = [accountSC titleForSegmentAtIndex:accountSC.selectedSegmentIndex];
[Name setText:name];
NSData *imageData = [dict objectForKey:#"ProfileImage"];
UIImage *imageProfile = [UIImage imageWithData:imageData];
[pictureButton setImage:imageProfile forState:UIControlStateNormal];
}
else {
Name.text = nil;
[pictureButton setImage:nil forState:UIControlStateNormal];
}
}
The first & second big block of code is temporary because I wanted to see the UIImage's based upon different objectAtIndex numbers.
Here is the console crash log:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString objectForKey:]: unrecognized selector sent to instance 0x2cc08'
Any reason why this can be happening? Do I need to post any other code I am using?
I really need help on this, I have been pulling my hair out!!!
Edit1:
I am using this code, also you were talking about isKindOfClass right?
Anyway this is the code:
NSDictionary *dict = [self.accounts objectAtIndex:1];
if ([dict isKindOfClass:[NSDictionary class]]) {
NSLog(#"YES");
}
else {
NSLog(#"NO");
}
Im testing now...
You're sending objectForKey: to an NSString object, specifically one that you've typed directly into your code somewhere in #"..." form rather than one you've created programmatically (or had created programmatically for you). Someone is putting something other than a dictionary into self.accounts.
NSDictionary: objectForKey:
objectForKey: returns the value associated with key, or nil if no value is associated with key.
key :A string identifying the value. If nil, just return nil.
(1)you should know the key is not blank
(2)you should know the object is not blank and the objects are the same type ,because you shouldn't use different kinds of type in a NSDictionary.
e.g. {#"key1":NSString,#"key2":NSArray} //this is a demonstration of error

Resources