iOS attempt to insert nil object from objects'

时间:2017-07-12 07:56:09

标签: ios xcode nsstring nsarray

I have this below error

[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[1]

It happens everytime I try to tap on the share button in one of my product post, and only if that post has a title (defined as item_title) that contains Chinese Characters.

The function of the share button is like this

-(void)shareBtnTapped
{

    NSArray * activityItems = @[[NSString stringWithFormat:@"%@",[delegate attributestringtostring:[[delegate.detailPageArray objectAtIndex:delegate.detailArraySelectedIndex]objectForKey:@"item_title"]]],[NSURL URLWithString:[[delegate.detailPageArray objectAtIndex:delegate.detailArraySelectedIndex]objectForKey:@"product_url"]]];
    NSArray * applicationActivities = nil;
    NSArray * excludeActivities = @[UIActivityTypeAssignToContact, UIActivityTypeCopyToPasteboard, UIActivityTypePostToWeibo, UIActivityTypePrint, UIActivityTypeMessage];

    UIActivityViewController * activityController = [[[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:applicationActivities] autorelease];
    activityController.excludedActivityTypes = excludeActivities;
    [self.view.window.rootViewController presentViewController:activityController animated:YES completion:nil];
    [Answers logCustomEventWithName:@"Item Page Tapped" customAttributes:@{@"Btn":@"Shared"}];

}

It works fine if the title is in English. I am very lost at the moment, and hoping if any experts here can shred some lights. Thank you so much.

Additional info: the item_title is saved as utf8mb4 in the database.

Problem solved: Thank you everyone for helping out! Since I know it is the url issue, I make the below changes to ensure the URL is defining the Chinese Character.

NSString* stringURL = [NSString stringWithFormat:[[delegate.detailPageArray objectAtIndex:delegate.detailArraySelectedIndex]objectForKey:@"product_url"]];
NSString* webStringURL = [stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL* url = [NSURL URLWithString:webStringURL];



NSArray * activityItems = @[obj1, url];

2 个答案:

答案 0 :(得分:0)

The problem is with this line:

NSArray * activityItems = @[[NSString stringWithFormat:@"%@",[delegate attributestringtostring:[[delegate.detailPageArray objectAtIndex:delegate.detailArraySelectedIndex]objectForKey:@"item_title"]]],[NSURL URLWithString:[[delegate.detailPageArray objectAtIndex:delegate.detailArraySelectedIndex]objectForKey:@"product_url"]]];

One of the items you're initializing array with turns out to be nil.

Try instead

id item1 = [NSString stringWithFormat:@"%@",[delegate attributestringtostring:[[delegate.detailPageArray objectAtIndex:delegate.detailArraySelectedIndex]objectForKey:@"item_title"]]];
id item2 = [NSURL URLWithString:[[delegate.detailPageArray objectAtIndex:delegate.detailArraySelectedIndex]objectForKey:@"product_url"]];
if (item1 != nil && item2 != nil) {
    NSArray * activityItems = @[item1, item2];
}

答案 1 :(得分:0)

one of your objects inserting into array is nil:

NSArray * activityItems = @[
[NSString stringWithFormat:@"%@",[delegate attributestringtostring:[[delegate.detailPageArray objectAtIndex:delegate.detailArraySelectedIndex]objectForKey:@"item_title"]]],
[NSURL URLWithString:[[delegate.detailPageArray objectAtIndex:delegate.detailArraySelectedIndex]objectForKey:@"product_url"]]
];

either its the line with NSString or line with NSURL. you could do something like this:

NSString *obj1 = [NSString stringWithFormat:@"%@",[delegate attributestringtostring:[[delegate.detailPageArray objectAtIndex:delegate.detailArraySelectedIndex]objectForKey:@"item_title"]]];
NSURL *obj2 =  [NSURL URLWithString:[[delegate.detailPageArray objectAtIndex:delegate.detailArraySelectedIndex]objectForKey:@"product_url"]];

NSArray * activityItems = @[obj1, obj2];

Then put a break point at NSArray * activityItems... and check obj1 and obj2 in the debugger window with po obj1 and po obj2 if one of them says nil then you will know where the problem is. One cannot put nil in an array. so make sure your object is not nil or if nil then don't add it to array.

edit:

your obji1, for example, is very complex. I would check if delegate or delegate.detailPageArray or delegate.detailArraySelectedIndex or objectForKey: is not returning nil. so you can with po command in debugger window and check all of these objects.