NSInvalidArgumentException, reason unrecognized selector sent to instance - ios

When processing a json response I a get the below error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[DADGList setDescription:]:
unrecognized selector sent to instance 0x7ae88880'
The class it crashed on:
#implementation DADGList
-(id)copy
{
DADGList *list = [[DADGList alloc] init];
list.identifier = self.identifier;
list.name = [self.name copy];
list.description = [self.description copy];
list.created = [self.created copy];
list.itemCount = self.itemCount;
list.shareLink = [self.shareLink copy];
return list;
}
+(DADGList *)listFromDictonary:(NSDictionary *)dictonary
{
DADGList *list = [[DADGList alloc] init];
NSLog( #"%#", dictonary );
list.identifier = [[dictonary objectForKey:#"list_id"] integerValue];
list.itemCount = [[dictonary objectForKey:#"list_items_count"] integerValue];
list.name = [NSString stringWithString:[dictonary objectForKey:#"list_name"]];
list.description = [NSString stringWithString:[dictonary objectForKey:#"list_description"]];
list.created = [[NSDate alloc] initWithTimeIntervalSince1970:[[dictonary objectForKey:#"list_created"] doubleValue]];
list.shareLink = [NSString stringWithString:[dictonary objectForKey:#"list_share_url"]];
return list;
}
and the dictonary that is past to listFromDictonary:

You should rename your description property for something else as this is a field already existing in the iOS echosystem (NSObject if I am not mistaken) and that creates all sort of weird crash like this.

If you are using core data maybe you need to implement the following instance:
Mall(entity: NSEntityDescription.entity(forEntityName: "Mall", in: context)!, insertInto: nil)
Where Mall is my entity and context is my view context.

Related

Unrecognized selector sent to instance Objective-C

I am receiving error:
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[__NSCFConstantString
subjectType]: unrecognized selector sent to instance
I am trying to sort students in my app to arrays by the subject type that they are learning.
AMStudent* student = [[AMStudent alloc] init];
NSMutableArray* studentArray = [[NSMutableArray alloc] init];
NSArray* studentNameArray = [NSArray arrayWithObjects: #"Student1", #"Student2", #"Student3", #"Student4", #"Student5", #"Student6", #"Student7", #"Student8", #"Student9", #"Student10", nil];
[studentArray addObjectsFromArray:studentNameArray];
for (NSInteger i = 0; i < [studentNameArray count]; i++) {
student.name = [studentNameArray objectAtIndex: i];
[student randomAnswer];
NSLog(#"%#", student.description);
}
NSMutableArray* techArray = [NSMutableArray array];
NSMutableArray* humArray = [NSMutableArray array];
for (AMStudent* stud in studentArray){
if ((stud.subjectType & AMStudentSubjectTypeDevelopment) | (stud.subjectType & AMStudentSubjectTypeMath)) {
[techArray addObject:stud];
} else {
[humArray addObject:stud];
}
}
I cant figure out what exactly I am doing wrong, because it crashes in this stage:
if ((stud.subjectType & AMStudentSubjectTypeDevelopment) | (stud.subjectType & AMStudentSubjectTypeMath)) {
[techArray addObject:stud];
} else {
[humArray addObject:stud];
}
You are calling
stud.subjectType
in the studentArray after copying the studentNames (NSString) to the student array:
[studentArray addObjectsFromArray:studentNameArray];
NSString won't recognize subjectType.
You fill studentArray using:
[studentArray addObjectsFromArray:studentNameArray];
So studentArray contains NSString instances. You then attempt to process the array using:
for (AMStudent* stud in studentArray){
This does not magically convert the NSString instances in studentArray into AMStudent instances. You don't get an error at this point as studentArray can contain objects of any type so the compiler just trusts you know what you are doing and places a reference to an NSString into stud. You then do:
if ((stud.subjectType ...
and this requires stud to reference an AMStudent object, which it does not, it references a (constant) string and so you get the error:
NSInvalidArgumentException', reason: '-[__NSCFConstantString subjectType]: unrecognized selector sent to instance
Instead of copying the names of the students into studentArray you need to create instances of AMStudent and add those to the array. Did you intend to do that in the first loop maybe?
HTH
techArray and humArray (NSArray) type change not working add object function.
NSMutableArray *newtechArray = [techArray mutableCopy];
NSMutableArray *newhumArray = [humarray mutableCopy];
if ((stud.subjectType & AMStudentSubjectTypeDevelopment) | (stud.subjectType & AMStudentSubjectTypeMath)) {
[newtechArray addObject:stud];
} else {
[newhumArray addObject:stud];
}
Thanks a lot for Your wide answer, I understood my mistake. Just added one more loop and added object student.
for (NSInteger numberOfStudents = 0; numberOfStudents < 10; numberOfStudents ++){
AMStudent* student = [[AMStudent alloc] init];
student.name = [studentNameArray objectAtIndex:numberOfStudents];
}
[studentArray addObject:student];

NSInvalidArgumentException', reason: '-[__NSArrayI length]: unrecognized selector sent to instance 0x165d5150'

Hi I am getting this data form server
NSDictionary*feed=[saveDic objectForKey:#"feed"];
NSLog(#"%#",feed); //Outputs: feed = ( { code = yQ7j0t; "user_id" = 889445341091863; } ); }
NSLog(#"%#",[feed valueForKey:#"code"]);
NSString *referralCode = [feed valueForKey:#"code"];
NSLog(#"%#",referralCode);
self.referralCode.text=referralCode;
And beacuse of that I am getting below error.
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI length]: selector sent to instance 0x165d5150'``
Any help will be appreciated.
The issue is, your feed key holds an array. You are not properly handling that in your code, that is why the crash occurs. When you call valueForKey: it retrieves an array of values held by that specific key.
For fixing that you can use:
NSArray *feed = [saveDic objectForKey:#"feed"];
NSArray *referralCodes = [feed valueForKey:#"code"];
NSString *referralCode = referralCodes.count ? referralCodes[0] : #"";
NSLog(#"%#",referralCode);
But I personally prefer using objectForKey: instead of valueForKey:. So you can re-write the code like:
NSArray *feed = [saveDic objectForKey:#"feed"];
NSString *referralCode = feed.count ? [feed[0] objectForKey:#"code"] : #"";
NSLog(#"%#",referralCode);
Some where you use a variable;
yourVaribleName.length
or
[yourVaribleName length]
which should be
yourVaribleName.count
note: the crash says exactly that "yourVaribleName" is NSArray type where you wants length of the NSArray. But NSArray has not feature "length". NSArray has "Count" feature
//try with this code bellow
NSArray *referralCode = [feed valueForKey:#"code"];
NSLog(#"%#",referralCode);
self.referralCode.text=[referralCode componentsJoinedByString:#" "];//#"," or #"" what event you need
Your feed data is in array. So you have retrieve code value from array.Hope it will help you.
NSMutableArray*feed=[saveDic objectForKey:#"feed"];
NSLog(#"%#",feed);
NSLog(#"%#",[feed valueForKey:#"code"]);
NSString *referralCode = [[feed objectAtIndex:indexPath]valueForKey:#"code"];
NSLog(#"%#",referralCode);
self.referralCode.text=referralCode;

Why is this NSString null in a FireBase query block?

I can't figure out why this string is null inside the FQuery block. My app keeps crashing when I build the dailyLog MutableDictionary at the user key;
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MMMM dd, YYYY"];
NSString *userID = [[self.userProfile objectForKey:#"userID"] copy];
self.logFirebase = [[Firebase alloc] initWithUrl:#"https://urlName.firebaseio.com/DailyLog"];
[[[self.logFirebase queryOrderedByPriority] queryEqualToValue:userID childKey:#"userID"] observeEventType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) {
if (![snapshot.value isEqual:[NSNull null]]) {
NSMutableArray *data = [CGCFirebaseDataExtractor extractKeysAndObjects:snapshot.value];
self.dailyLog = [data filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"Name CONTAINS[cd] %#",[df stringFromDate:[NSDate date]]]].lastObject;
}else{
self.dailyLog = [[NSMutableDictionary alloc] init];
NSLog(#"users Profile:%#",self.userProfile);
NSLog(#"users ID :%#",[self.userProfile objectForKey:#"userID"]);
[self.dailyLog setObject:[df stringFromDate:[NSDate date]] forKey:#"date"];
[self.dailyLog setObject:[self.userProfile objectForKey:#"userID"] forKey:#"user"];
[self.dailyLog setObject:[NSNumber numberWithInt:450] forKey:#"calories"];
[[self.logFirebase childByAutoId] setValue:self.userProfile];
}
}];
EDIT:
When I log self.userProfile, I get the proper info I want. But when I log the userID itself from within the FQuery block, it's null
Here is my updated crash data:
2015-05-08 13:21:22.709 <appname>[4160:1321097] users Profile:{
"-JoirTqCXFFDY1psLTNn" = {
dateRegistered = "1431010405.81792";
userID = "304D92EF-CE77-4A10-A55F-9847153699F7";
userName = "User's Name";
};
}
2015-05-08 13:21:22.709 <appname>[4160:1321097] users ID :(null)
2015-05-08 13:21:22.714 <appname>[4160:1321097] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: object cannot be nil (key: user)'
*** First throw call stack:
(0x1852082d8 0x196a340e4 0x1850f1428 0x10007de00 0x1001454d0 0x100390fd4 0x100390f94 0x100395c28 0x1851bf7f8 0x1851bd8a0 0x1850e92d4 0x18e8ff6fc 0x189caefac 0x10007f14c 0x1970b2a08)
libc++abi.dylib: terminating with uncaught exception of type NSException
Sorry, I'm an idiot and didn't look closely at your log.
2015-05-08 13:21:22.709 <appname>[4160:1321097] users Profile:{
"-JoirTqCXFFDY1psLTNn" = {
dateRegistered = "1431010405.81792";
userID = "304D92EF-CE77-4A10-A55F-9847153699F7";
userName = "User's Name";
};
This object is evidently a dictionary in a dictionary. The first (outer) dictionary has key "-JoirTqCXFFDY1psLTNn" and that is its only key. That is why trying to get the key userID fails; that key is part of the second (inner) dictionary.

Extremely High Anonymous VM and App Crash

I have a really big problem that I can't handle. My app uses iBeacons to show views and load some pics stored in CoreData, but well, thats not the real question.
I'm loading some pics from parse to core data then retrieved from core data to be displayed.
When i change from one view to another i assume that it can increase, but when i quit the view could be possible to release memory? I don't know it I'm explaining it very well, but i hope you can understand me and help me as much as you can.
I'm using Xcode 5.1.1 and iOS 7.1 on my iPad
Now I'm using instruments to try to see what's going on, and this is what i get.
Allocations:
I think i'm not doing it wrong, loading images like this, dunno what is the problem :_(
self.itemsSection1 = [[NSMutableArray alloc] init];
self.generalImages1 = [[NSMutableArray alloc] init];
[self fillArraysWithItems:self.itemsSection1 section:#"1"];
[self fillArraysWithGeneralImages:self.generalImages1 section:#"1"];
self.itemImages = [NSArray
arrayWithObjects:[UIImage imageWithData:self.itemsSection1[0][#"image"]],
[UIImage imageWithData:self.itemsSection1[1][#"image"]],
[UIImage imageWithData:self.itemsSection1[2][#"image"]],
[UIImage imageWithData:self.itemsSection1[3][#"image"]],
[UIImage imageWithData:self.itemsSection1[4][#"image"]], nil];
for (int i=0; i<self.generalImages1.count; i++) {
[self.itemSliderImages addObject:[UIImage imageWithData:self.generalImages1[i][#"image"]]];
}
generalImage.image = [UIImage imageWithData:self.generalImages1[0][#"image"]];
self.img_elements.image = generalImage.image;
self.navBar_title.title = #"Catering";
FillArraysWithItems:
-(void)fillArraysWithItems: (NSMutableArray*)item section:(NSString*)section{
AppDelegate *delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Item" inManagedObjectContext:delegate.managedObjectContext];
NSFetchRequest *fetchedRequest = [[NSFetchRequest alloc]init];
[fetchedRequest setEntity:entity];
self.allItemInfo = [[delegate.managedObjectContext executeFetchRequest:fetchedRequest error:nil] mutableCopy];
NSLog(#"\n\nAllItems:%#\n", self.allItemInfo);
for (NSManagedObjectContext *obj in self.allItemInfo) {
//Save data from CoreData to arrays
NSLog(#"\n\nObject:\n%#", obj);
NSString *micro = #"micro_off.png";
NSString *oven = #"oven_off.png";
NSString *recycle = #"recycle_off.png";
NSString *biodeg = #"biodeg_off.png";
NSString *liquid = #"liquid_off.png";
NSString *perso = #"perso_off.png";
NSLog(#"\n\nName:%#\nRef:%#\nPricePack:%#\nUnitPrice:%#\nunitsPack:%#",[obj valueForKey:#"name"], [obj valueForKey:#"reference"],[obj valueForKey:#"pricePack"], [obj valueForKey:#"priceUnity"], [obj valueForKey:#"unitsPack"]);
NSLog(#"\n\nMicro:%#\nOven:%#\nRecycle:%#\nBiodeg:%#\nLiquid:%#\nPerso:%#\n", [obj valueForKey:#"micro"], [obj valueForKey:#"oven"], [obj valueForKey:#"recycle"], [obj valueForKey:#"bio"], [obj valueForKey:#"liquid"], [obj valueForKey:#"perso"]);
if ([[obj valueForKey:#"section"] isEqual:section]) {
if ([[obj valueForKey:#"micro"] isEqual: #1]) {
micro = #"micro_on.png";
}
if ([[obj valueForKey:#"oven"] isEqual: #1]) {
oven = #"oven_on.png";
}
if ([[obj valueForKey:#"recycle"] isEqual: #1]) {
recycle = #"recycle_on.png";
}
if ([[obj valueForKey:#"bio"] isEqual: #1]) {
biodeg = #"biodeg_on.png";
}
if ([[obj valueForKey:#"liquid"] isEqual: #1]) {
liquid = #"liquid_on.png";
}
if ([[obj valueForKey:#"perso"] isEqual: #1]) {
perso = #"perso_on.png";
}
[item addObject:#{#"name": [obj valueForKey:#"name"],
#"pricePack": [obj valueForKey:#"pricePack"],
#"priceUnity": [obj valueForKey:#"priceUnity"],
#"reference": [obj valueForKey:#"reference"],
#"unitsPack": [obj valueForKey:#"unitsPack"],
#"micro": [UIImage imageWithContentsOfFile:micro],
#"oven": [UIImage imageWithContentsOfFile:oven],
#"recycle": [UIImage imageWithContentsOfFile:recycle],
#"bio": [UIImage imageWithContentsOfFile:biodeg],
#"liquid": [UIImage imageWithContentsOfFile:liquid],
#"perso": [UIImage imageWithContentsOfFile:perso],
#"image": [UIImage imageWithContentsOfFile:[obj valueForKey:#"image"]]}];
}
}
NSError *error;
BOOL isFetched = [delegate.managedObjectContext save:&error];
NSLog(#"Arrays successfully filled: %d", isFetched);
}
And this is the error I get when [item addObject...
2014-06-18 09:25:50.176 IBKS[383:60b] -[_PFExternalReferenceData stringByDeletingPathExtension]: unrecognized selector sent to instance 0x14d51f50
2014-06-18 09:25:50.178 IBKS[383:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_PFExternalReferenceData stringByDeletingPathExtension]: unrecognized selector sent to instance 0x14d51f50'
*** First throw call stack:
(0x2f8f1f0b 0x3a3cece7 0x2f8f5837 0x2f8f4137 0x2f843098 0x3213d13f 0x3213d107 0x3213d0dd 0x3213d0ab 0x322487e5 0xd2fe5 0xa76a5 0x32125a53 0x32125811 0x321cf043 0x3220bc4b 0x3220a57d 0x322095f9 0xa01cb 0x9debf 0x2fdb6e91 0x2fdb1aeb 0x2fdab081 0x2f8bd01d 0x2f8bc397 0x2f8bb001 0x2f825769 0x2f82554b 0x3475f6d3 0x32184891 0x9a5ed 0x3a8ccab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
Hope anyone can help.
Thanks

adding Json to mutable array resolves in crash

Hello guys I am new to Xcode/iOS developing
I trying to add json data to the mutable array , and it results in app crash :(
so far here is my code:
if(! [defaults objectForKey:#"Person1"])
[defaults setObject:[PersonsFromSearch objectAtIndex:index] forKey:#"Person1"];
else
{
NSMutableArray *Array = [[NSMutableArray alloc]init];
id object = [defaults objectForKey:#"Person1"];
Array = [object isKindOfClass:[NSArray class]] ? object : #[object];
[Array addObject:[PersonsFromSearch objectAtIndex:index]];//crash here :((
[Array moveObjectFromIndex:[Array count] toIndex:0];
}
Crash Dump:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI addObject:]: unrecognized selector sent to instance 0xcb4d380'
* First throw call stack:
what is wrong here ? can you please help me to resolve this issue
Array contains this (Json?)
{
Address = "\U05d3\U05e8\U05da \U05d4\U05e9\U05dc\U05d5\U05dd 53";
CellPhone = "052-3275381";
EMail = "editor#pc.co.il";
EnglishPerson = "Yehuda Konfortes";
FaceBookLink = "";
Fax1 = "03-7330703";
Fax2 = "";
FileNAme = "100050.jpg";
HomeEMail = "";
HomeFax = "";
HomePhone1 = "";
HomePhone2 = "";
PersonID = 100050;
PersonName = "\U05d9\U05d4\U05d5\U05d3\U05d4 \U05e7\U05d5\U05e0\U05e4\U05d5\U05e8\U05d8\U05e1";
Phone1 = "03-7330733";
Phone2 = "";
ZipCode = "";
}
[defaults objectForKey:#"Person1"]; returns dictionary but not array.
So you can't use addObject method.
UPD
You may resolve this crash by creation array with a single object.
Here is updated code:
NSMutableArray *Array = [[NSMutableArray alloc]init];
id object = [defaults objectForKey:#"Person1"];
Array = [object isKindOfClass:[NSArray class]] ? [object mutableCopy] : [#[object] mutableCopy];
[Array addObject:[PersonsFromSearch objectAtIndex:index]];//crash here :((
[Array moveObjectFromIndex:[Array count] toIndex:0];
NSMutableArray *_arr = [[NSMutableArray alloc]init];
NSDictionary *results = PASS JSON Value;
[_arr add object:[results objectforkey:#“Address”]];
[_arr add object:[results objectforkey:#“CellPhone”]];

Resources