本文介绍了我的应用程序因此错误而崩溃 - 'NSInvalidArgumentException'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个程序来检索 JSON 文件并实现了它
I have created a program to retrieve JSON file and it achieved it
NSString *FilePath = [[NSBundle mainBundle]pathForResource:@"Message" ofType:@"json"];
NSData *data = [NSData dataWithContentsOfFile:FilePath];
NSError *error;
if(error){
NSLog(@"Error and CAn't retrive data: %@", error.localizedDescription);
}else{
NSDictionary * jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"Your Json Dictionary values are %@", jsonDict);
for(NSDictionary *valuesDictionary in jsonDict){
ShopCollectionObject *shopObject = [[ShopCollectionObject alloc]initWithID:[[valuesDictionary objectForKey:@"message_id"]integerValue] Name:[valuesDictionary objectForKey:@"product"] TimeAsPrice:[[valuesDictionary objectForKey:@"message_time"]integerValue] Avathar:[valuesDictionary objectForKey:@"item_image"] user:[valuesDictionary objectForKey:@"user_image"] Name_User:[valuesDictionary objectForKey:@"user_name"] LocationOfUser:[valuesDictionary objectForKey:@"locate_user"]];
但我的应用程序在这里崩溃并出现上述错误
But My app crashes here with the above error
[self.objectForArray addObject:shopObject];
}
}
在下面更新了我的商店收藏代码
Updated my shop collection code below
Shopcollection object.h
Shopcollection object.h
#import <Foundation/Foundation.h>
@interface ShopCollectionObject : NSObject
-(instancetype) initWithID: (int)msgID Name:(NSString *)Profile_name TimeAsPrice:(int) GivenTimeAsPrice Avathar:(NSString *) PhotoOfAvathar user:(NSString *)UserAvathar Name_User: (NSString *) UserNames LocationOfUser:(NSString *) USerLocationGiven;
@property (nonatomic) int msgID;
@property(nonatomic, strong)NSString* Name;
@property (nonatomic) int TimeAsPrice;
@property (nonatomic,strong) NSString* Avathar;
@property (nonatomic,strong) NSString* user;
@property (nonatomic,strong) NSString* Name_User;
@property(nonatomic,strong) NSString* LocationOfUser;
@end
Shopcollectionobject.m
Shopcollectionobject.m
#import "ShopCollectionObject.h"
@implementation ShopCollectionObject
-(instancetype)initWithID:(int)msgID Name:(NSString *)Profile_name TimeAsPrice:(int)GivenTimeAsPrice Avathar:(NSString *)PhotoOfAvathar user:(NSString *)UserAvathar Name_User:(NSString *)UserNames LocationOfUser:(NSString *)USerLocationGiven{
self = [super init];
if(self){
self.msgID = msgID;
self.Name = Profile_name;
self.TimeAsPrice = GivenTimeAsPrice;
self.Avathar = PhotoOfAvathar;
self.user = UserAvathar;
self.Name_User = UserNames;
self.LocationOfUser = USerLocationGiven;
}
return self;
}
@end
推荐答案
ShopCollectionObject.h
ShopCollectionObject.h
#import <Foundation/Foundation.h>
@interface ShopCollectionObject : NSObject
@property (nonatomic) int message_id;
@property (strong, nonatomic) NSString *Name;
@property (nonatomic) int TimeAsPrice;
@property (strong, nonatomic) NSString *Avathar;//user,Name_User,LocationOfUser,message_id
@property (strong, nonatomic) NSString *user;
@property (strong, nonatomic) NSString *Name_User;
@property (strong, nonatomic) NSString *LocationOfUser;
-(instancetype) initWithID: (int)msgID Name:(NSString *)Profile_name TimeAsPrice:(int) GivenTimeAsPrice Avathar:(NSString *) PhotoOfAvathar user:(NSString *)UserAvathar Name_User: (NSString *) UserNames LocationOfUser:(NSString *) USerLocationGiven;
@property (nonatomic) int msgID;
@end
ShopCollectionObject.m
ShopCollectionObject.m
#import "ShopCollectionObject.h"
@implementation ShopCollectionObject
-(instancetype)initWithID:(int)msgID Name:(NSString *)Profile_name TimeAsPrice:(int)GivenTimeAsPrice Avathar:(NSString *)PhotoOfAvathar user:(NSString *)UserAvathar Name_User:(NSString *)UserNames LocationOfUser:(NSString *)USerLocationGiven{
self = [super init];
if(self){
self.msgID = msgID;
self.Name = Profile_name;
self.TimeAsPrice = GivenTimeAsPrice;
self.Avathar = PhotoOfAvathar;
self.user = UserAvathar;
self.Name_User = UserNames;
self.LocationOfUser = USerLocationGiven;
}
return self;
}
@end
ViewController.m
ViewController.m
#import "ViewController.h"
#import "ShopCollectionObject.h"
@interface ViewController ()
{
NSMutableArray *objectForArray;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
objectForArray = [[NSMutableArray alloc]init];
NSString *FilePath = [[NSBundle mainBundle]pathForResource:@"Message" ofType:@"json"];
NSData *data = [NSData dataWithContentsOfFile:FilePath];
NSError *error;
if(error){
NSLog(@"Error and CAn't retrive data: %@", error.localizedDescription);
}else{
NSDictionary * jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
for(NSDictionary *valuesDictionary in jsonDict){
ShopCollectionObject *shopObject = [[ShopCollectionObject alloc]initWithID:[[valuesDictionary objectForKey:@"message_id"]intValue] Name:[valuesDictionary objectForKey:@"product"] TimeAsPrice:[[valuesDictionary objectForKey:@"message_time"]intValue] Avathar:[valuesDictionary objectForKey:@"item_image"] user:[valuesDictionary objectForKey:@"user_image"] Name_User:[valuesDictionary objectForKey:@"user_name"] LocationOfUser:[valuesDictionary objectForKey:@"locate_user"]];
[objectForArray addObject:shopObject];
}
NSLog(@"%@",objectForArray);
ShopCollectionObject *data = objectForArray[0];
NSLog(@"%@",data.Name);
}
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
请检查此代码
这篇关于我的应用程序因此错误而崩溃 - 'NSInvalidArgumentException'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!