-2

I am trying to use a text field to type in some numbers and then have the Parse server look for images tagged with whatever number was typed in the field, but I am getting this big red error (in the subject header) when trying to build the app for testing. Here's my code:

-(IBAction)Submit:(id)sender {

PFFile *people = TicketNumberBox.text[@"ticketNumber"];
[people getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
    if (!error) {
         UIImageView TicketImage = [UIImage imageWithData:imageData];
    }
}];

}

Here's an image to help you further: enter image description here

TheEliteTech
  • 147
  • 3
  • 11
  • `TicketNumberBox` is what? a `UITextField`? A `UILabel`? So `TicketNumberBox.text` is a `NSString`, and not a `NSDictionary`. You can't do `TicketNumberBox.text[@"ticketNumber"]`. – Larme Feb 11 '15 at 11:14
  • Yes. It is a Text Field like I said before. – TheEliteTech Feb 11 '15 at 11:22
  • `PFFile *people = TicketNumberBox.text;`? – Larme Feb 11 '15 at 11:30
  • The convention is to begin the names of variables with a lowercase letter and the names of classes with an uppercase letter. Under that Objective-C convention `TicketNumberBox` looks line class name and causes some confusion. To make things more clear you can write: `-(IBAction)Submit:(NSTextField *)sender` and use `sender` as the textfield pointer: `self.text`. – zaph Feb 11 '15 at 11:44
  • `[@"ticketNumber"]` would be used to reference an element of an NSDictionary. An NSString is not an NSDictionary. – Hot Licks Feb 11 '15 at 19:02

2 Answers2

1

TicketNumberBox.text[@"ticketNumber"]; doesn't make any sense as text variable from UITextField is type of NSString, not dictionary, so You can't just take ticketNumber value like that. You can divide your code to something like this to understand:

NSString *text = TicketNumberBox.text;
PFFile *people = text[@"ticketNumber"];

Is that still makes sense to You?

More believable would be:

NSString *text = TicketNumberBox.text;
PFFile *people = [PFFile alloc] init];
people.ticketNumber = text;
Paulius Vindzigelskis
  • 2,121
  • 6
  • 29
  • 41
1

The syntax in someString[@"this is the string I want to set"] is how you request a value-for-key @"this is the string I want to set" in NSDictionary, which is why you're getting the specific error you got. However, a string isn't a dictionary, so the compiler is complaining that you're trying to address a string as a dictionary key. The element of confusion is that dictionaries can use a string as a key. So to reference a the dictionary: NSDictionary *dictionary = @{@"key":"@"value"}; you use the syntax NSString *string = dictionary[@"key"] which returns @"value", making "string" equal to @"value".

markedwardmurray
  • 523
  • 4
  • 10