-1

I am very new to iOS development I have come across an issue where the label does not want to display the value in an NSArray. Please could someone help point out where it is wrong and why?

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

    json_data = [json objectForKey:@"data"];

    content = [json_data objectForKey:@"content"];

    NSMutableArray *contentTemp = [[NSMutableArray alloc] initWithCapacity:content.count];

    for (NSDictionary *contentInfo in content) {

        project_details *mvm = [[project_details alloc] init];
        mvm.bname = contentInfo[@"bname"];
        mvm.burl = contentInfo[@"burl"];
        mvm.p_num = contentInfo[@"p_id"];
        mvm.pname = contentInfo[@"pname"];
        mvm.pdesc = contentInfo[@"pdesc"];
        mvm.purl = contentInfo[@"purl"];
        mvm.plive = contentInfo[@"plive"];
        mvm.pcompleted = contentInfo[@"pcompleted"];
        mvm.days = contentInfo[@"days"];
        mvm.pfunds = contentInfo[@"pfunds"];
        mvm.amount = contentInfo[@"amount"];
        mvm.perc = contentInfo[@"perc"];

        [contentTemp addObject:mvm];

    }

    self.projectArray = contentTemp;

    dispatch_async(dispatch_get_main_queue(), ^{
         [self.label setText:[NSString stringWithFormat:@"%@", [self.projectArray valueForKey:@"bname"]]];

          NSLog(@"%@", [self.projectArray valueForKey:@"bname"]);
    });


}];

[dataTaskProject resume];

What I am getting when I run the code: NSLog prints the correct value but the setText method prints "(" to the label.

This is the code as is I did not put the URL in and there are no errors when running the code.

projectArray is defined in the .h file as: @property (nonatomic, copy) NSArray *projectArray;

This is what is printed in NSLog:

2016-06-06 18:28:10.281 #####[37127:1228517] (
"Global Giving" )
Alexander
  • 1,424
  • 18
  • 23
  • what is the correct value and what is the printed value ? NSLOG the self.label.text and see what it logs. – Teja Nandamuri Jun 06 '16 at 16:19
  • 1
    What's the kind of value for `[self.projectArray valueForKey:@"bname"]`? I guess that's the NSArray, and your label show only the first line. – Larme Jun 06 '16 at 16:28
  • The correct value for @"bname" is Global Giving. In NSLog it prints "Global Giving", – Alexander Jun 06 '16 at 16:29
  • The label prints "(" – Alexander Jun 06 '16 at 16:30
  • I did think maybe it is just showing "("because the label was not large enough, so I made the label larger but it still shows only "(". – Alexander Jun 06 '16 at 16:33
  • If I rewrite as: [self.label setText:[NSString stringWithFormat:@"Hello"]]; the label prints "Hello" – Alexander Jun 06 '16 at 16:34
  • 1
    Calling `valueForKey:` on an `NSArray` gives you back another `NSArray`. Why are you calling `valueForKey:` on the `projectArray`? Get the right data first, then put that proper value on the label. – rmaddy Jun 06 '16 at 16:46
  • @Alexander - Yep, that's an array with one item and you're just seeing the `(` at the start of a string representation of an array. You've built an array of peoject details objects and then asked the label to hold the array of all of those bname values. That doesn't make sense. So what do you want? The first one? A comma or semicolon separated list of them? – Rob Jun 06 '16 at 16:56
  • @rmaddy Would you be able to please show me how to "extract" the value correctly? I assume I will need to add a line after `self.projectArray = contentTemp;`. Not sure what to add, do I place it into an NSString? – Alexander Jun 06 '16 at 16:58
  • It all depends on what you want to show in the label. You have an array of `project_details` instances. But you have one label. What should be in the label? The `bname` of all projects? The `bname` of a specific project? – rmaddy Jun 06 '16 at 17:01
  • Unrelated - It is common coding practice that variable and method names start with lowercase letters. Class names start with uppercase letters. All names should use camel case. So your `project_details` class should be named `ProjectDetails`. – rmaddy Jun 06 '16 at 17:02
  • @Rob the JSON object returns 1 object with those keys named exactly as above. What I did was assign them into a class with the same names Keys (I am not sure if I am coming across right) and then I want to use it to display in a separate label. (this example only being 1 label, seeing as I can't even get one to display). I used the same logic and code and it populated a table view perfectly, I just don't know what I have done wrong here besides what `rmaddy` has commented which sounds correct. – Alexander Jun 06 '16 at 17:04
  • @rmaddy It should display the `bname` of 1 specific project but the api is only returning 1 project there are no others hence there isn't any validation code yet. In fact validation may be done in the api itself. – Alexander Jun 06 '16 at 17:06
  • If you only have one project, why have an array? The answer by robmathers tells you what to do if you decide to keep the array. – rmaddy Jun 06 '16 at 17:08
  • @rmaddy It is a good point thank you for the help. Will definitely review logic. – Alexander Jun 06 '16 at 17:55

1 Answers1

0

Calling valueForKey on an NSArray returns an NSArray. What you likely want instead is [(mvm *)self.projectArray.firstObject bname]. Your self.projectArray contains one or more mvm objects, and those are what have the bname property.

You can change .firstObject to an array index (i.e. [1], [2], etc.), depending what you want to access in the array (it's not clear to me from your code).

robmathers
  • 3,028
  • 1
  • 26
  • 29