0

I am working with the following function atm, but I'm banging my head against a wall.

-(double)fetchTimeUntilNextUpdateInSeconds{
    NSFetchRequest *fetchReq = [[NSFetchRequest alloc]initWithEntityName:@"DataInfo"];
    fetchReq.predicate = [NSPredicate predicateWithFormat:@"data_info_id == 1"];
    [fetchReq setPropertiesToFetch:[NSArray arrayWithObject:@"nextupdate"]];
    NSArray *array = [self.context executeFetchRequest:fetchReq error:nil];
    NSString *string = [[array valueForKey:@"nextupdate"] stringValue];
    NSLog(@"string: %@     array count:%lu", string, (unsigned long)array.count);
    NSArray *hoursAndMins = [string componentsSeparatedByString:@":"];
    int hours = [hoursAndMins[0] intValue];
    int mins = [hoursAndMins[1] intValue];

    return (mins*60)+(hours*60*60);
}

LOG: string: ( "05:42" ) array count:1

I'm getting following error: -[__NSArrayI componentsSeparatedByString:]: unrecognized selector sent to instance 0x174224060'

fair enough, i try to invoke "stringValue" method on string (as showed in code snippet) and get the following instead: -[__NSArrayI stringValue:]: unrecognized selector sent to instance 0x174224060'

The ladder makes me think I'm already receiving a string as stringValue is not a method of that class.... but why won't the first work then. Better yet, what am I doing wrong here?

7
  • 1
    Do you understand the errors?? [NSArray unrecognized selector Commented Aug 9, 2015 at 15:35
  • 1
    Your log line seems to be saying that the thing you're calling string is really an array with one element. (See the parentheses around the string.) That matches the errors you're getting when you attempt to treat an array as a string. Commented Aug 9, 2015 at 15:37
  • @user1967709 a rather strange question i would say.... yes i do... which i think is obvious based on my last sentences Commented Aug 9, 2015 at 15:39
  • 1
    Making sure, so when you used stringValue that only worked out okay bc the method - valueForKey: returns an type id therefore allowing you to use stringValue as you an tell there is no stringValue for the NSArray class. I'd suggest to you to print your elements after each line. Either save your array locally so you can see it better, or use the debugger to focus with break points, bc it looks like you haven't "gotten to the string value" you're still pointing to an array Commented Aug 9, 2015 at 15:43
  • @phillip mills I been doing all kind of ways, and I did also try to access index 0 of the array before; I did it in a wrong way. As nasty as having a nested array with only 1 object is, I think I got it fixed. Thx a lot for waking me up, I think I still need some detox from yesterday:P Commented Aug 9, 2015 at 15:46

2 Answers 2

1

I guess, executeFetchRequest returns an array containing always one item.

The mistake is the method valueForKey which is ambiguous. It's a key value coding method as well as a method of NSManagedObject. If you want to get the value of a key of one object, so first get the first object from the array and then call valueForKey.

NSArray *array = [self.context executeFetchRequest:fetchReq error:nil];
// get the value of the key `nextUpdate` of the first item of the array
NSString *string = [array[0] valueForKey:@"nextupdate"];

To make clear what's happening when valueForKey is sent to an array, see this code, it returns an array of the values for the key id of all members of the array.

NSArray *array = @[@{@"name" : @"John", @"id" : @"1"}, @{@"name" : @"Jane", @"id" : @"2"}];
NSLog(@"%@", [array valueForKey:@"id"]); // --> @[@"1", @"2"]
Sign up to request clarification or add additional context in comments.

2 Comments

hmm, Im actually getting unrecognized selector objectForKey error with this... I think it might be because objectForKey might not be implemented for NSManagedObject. When I see your answer I thought, of course and how smooth, but since it don't work in this case, Ill have to stick with this silly code that works: NSArray *array = [self.context executeFetchRequest:fetchReq error:nil]; NSArray *array2 = [array valueForKey:@"nextupdate_clouded"]; NSString *string = array2[0];
then use valueForKey but after flattening the array. I edited the post
0

Uhh, could also be the case that ( "05:42" ) has quotation marks that you may need to escape before you write this as a string to an array. OR you just maybe need to typecast the value of string AGAIN, but instead of doing that, why not try this first and tell us what happens.

NSArray *hoursAndMins = [[[array valueForKey:@"nextupdate"] stringValue] componentsSeparatedByString:@":"];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.