NSMutableArray的NSInternalInconsistencyException(NSInternalInconsistencyException for NSMutableArray)
我正在尝试将对象添加到NSMutableArray但它一直给我这个错误:
NSInternalInconsistencyException', reason: '-[__NSCFArray insertObject:atIndex:]: mutating method sent to immutable object
我已经研究过这个问题了,我没有做过去做过的任何错事,所以我不知道出了什么问题。 这是我的代码:
Group.h
@property (strong, nonatomic) NSString *custom_desc; @property (strong, nonatomic) NSMutableArray *attributes; //I define the array as mutable
Group.m
#import "Group.h" @implementation Group -(id)init { self = [super init]; if(self) { //do your object initialization here self.attributes = [NSMutableArray array]; //I initialize the array to be a NSMutableArray } return self; } @end
GroupBuilder.m
#import "GroupBuilder.h" #import "Group.h" @implementation GroupBuilder + (NSArray *)groupsFromJSON:(NSData *)objectNotation error:(NSError **)error { NSError *localError = nil; NSDictionary *parsedObject = [NSJSONSerialization JSONObjectWithData:objectNotation options:0 error:&localError]; if (localError != nil) { *error = localError; return nil; } NSMutableArray *groups = [[NSMutableArray alloc] init]; NSDictionary *results = [parsedObject objectForKey:@"result"]; NSArray *items = results[@"items" ]; for (NSDictionary *groupDic in items) { Group *group = [[Group alloc] init]; for (NSString *key in groupDic) { if ([group respondsToSelector:NSSelectorFromString(key)]) { [group setValue:[groupDic valueForKey:key] forKey:key]; } } [groups addObject:group]; } for(NSInteger i = 0; i < items.count; i++) { //NSLog(@"%@", [[items objectAtIndex:i] objectForKey:@"attributes"]); NSMutableArray *att = [[items objectAtIndex:i] objectForKey:@"attributes"]; //this returns a NSArray object understandable Group *g = [groups objectAtIndex:i]; [g.attributes addObjectsFromArray:[att mutableCopy]]; //I use mutable copy here so that i'm adding objects from a NSMutableArray and not an NSArray } return groups; } @end
I'm trying to add objects to an NSMutableArray but it keeps giving me this error.:
NSInternalInconsistencyException', reason: '-[__NSCFArray insertObject:atIndex:]: mutating method sent to immutable object
I have researched this problem, and I'm not doing anything wrong that past people have done, so I have no idea what's wrong. Here is my code:
Group.h
@property (strong, nonatomic) NSString *custom_desc; @property (strong, nonatomic) NSMutableArray *attributes; //I define the array as mutable
Group.m
#import "Group.h" @implementation Group -(id)init { self = [super init]; if(self) { //do your object initialization here self.attributes = [NSMutableArray array]; //I initialize the array to be a NSMutableArray } return self; } @end
GroupBuilder.m
#import "GroupBuilder.h" #import "Group.h" @implementation GroupBuilder + (NSArray *)groupsFromJSON:(NSData *)objectNotation error:(NSError **)error { NSError *localError = nil; NSDictionary *parsedObject = [NSJSONSerialization JSONObjectWithData:objectNotation options:0 error:&localError]; if (localError != nil) { *error = localError; return nil; } NSMutableArray *groups = [[NSMutableArray alloc] init]; NSDictionary *results = [parsedObject objectForKey:@"result"]; NSArray *items = results[@"items" ]; for (NSDictionary *groupDic in items) { Group *group = [[Group alloc] init]; for (NSString *key in groupDic) { if ([group respondsToSelector:NSSelectorFromString(key)]) { [group setValue:[groupDic valueForKey:key] forKey:key]; } } [groups addObject:group]; } for(NSInteger i = 0; i < items.count; i++) { //NSLog(@"%@", [[items objectAtIndex:i] objectForKey:@"attributes"]); NSMutableArray *att = [[items objectAtIndex:i] objectForKey:@"attributes"]; //this returns a NSArray object understandable Group *g = [groups objectAtIndex:i]; [g.attributes addObjectsFromArray:[att mutableCopy]]; //I use mutable copy here so that i'm adding objects from a NSMutableArray and not an NSArray } return groups; } @end
原文:https://stackoverflow.com/questions/23721380
满意答案
Mongo
的函数是异步的。 在connect
回调中的任何代码完成之前,您正在关闭db
连接。在
insert
的回调中更改db.close()
调用。router.post('/adduser', function (req, res, next) { var _username = req.body.username; var _password = req.body.password; var url = 'mongodb://localhost:27017/interview_prac'; MongoClient.connect(url, function (err, db) { //if we didn't connect, throw error if (err) throw err; console.log("connected") var users = db.collection('Users') users.findOne({ name: _username }, function (err, user) { if (user) { err = 'The username already exists' res.render('about', { msg: err }) } else { users.insert({ name: _username, password: _password }, function (err, result) { db.close() console.log("Database closed") console.log("entry saved") var new_msg = "Welcome " + _username res.render('about', { msg: new_msg }) }) } }) }) })
Mongo
's functions are asynchronous. You're closing thedb
connection before it has even done any of the code inside yourconnect
callback.Change your
db.close()
call inside theinsert
's callback.router.post('/adduser', function (req, res, next) { var _username = req.body.username; var _password = req.body.password; var url = 'mongodb://localhost:27017/interview_prac'; MongoClient.connect(url, function (err, db) { //if we didn't connect, throw error if (err) throw err; console.log("connected") var users = db.collection('Users') users.findOne({ name: _username }, function (err, user) { if (user) { err = 'The username already exists' res.render('about', { msg: err }) } else { users.insert({ name: _username, password: _password }, function (err, result) { db.close() console.log("Database closed") console.log("entry saved") var new_msg = "Welcome " + _username res.render('about', { msg: new_msg }) }) } }) }) })
相关问答
更多MongoDB - 如果不存在则插入,否则跳过(MongoDB- Insert if it doesn't exist, else skip)
MongoDB - 如何查询集合中的嵌套项目?(MongoDB - how to query for a nested item inside a collection?)
Mongodb不会将项目插入集合中(Mongodb doesn't insert item into collection)
php-insert数组中的mongodb到mongo db集合(mongodb in php-insert array to mongo db collection)
无法使用Meteor在MongoDB中创建空集合(Can not create empty collection in MongoDB using Meteor)
在mongodb db中解析错误,插入到具有唯一索引的集合中(Parsing error in mongodb db, insert to collection with unique index)
PHP中的MongoDB - 如何将项目插入到集合中的数组中?(MongoDB in PHP - How do I insert items into an array in a collection?)
检查项目集合值mongodb(check an item collection value mongodb)
MongoDB - 如何将位置插入集合(MongoDB - how to insert a location into collection)
如何从集合中插入多个文档作为另一个文档中的数组字段 - MongoDB(How to insert multiple documents from a collection as an array field in another document - MongoDB)
相关文章
更多IOS高访微信聊天对话界面(sizeWithFont:constrainedToSize和stretchableImageWithLeftCapWidth的使用)
最新问答
更多获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
如何通过引用返回对象?(How is returning an object by reference possible?)
矩阵如何存储在内存中?(How are matrices stored in memory?)
每个请求的Java新会话?(Java New Session For Each Request?)
css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
xcode语法颜色编码解释?(xcode syntax color coding explained?)
在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!