0

Heyo Guys

So I am more or less new to Objective C and got to a problem which I seem unable to solve.

I created a class called "Students" which all have a name surname etc. All those Students are put into a NSMutableArray (before they get created from JSON , that seems to work without a problem though). Then all the names of the students are put into a ListView. Afterwards when a name is clicked (segue passes the object), one should see the details of said student (i.e. his full name, his id, his street). The problem is that all the string values of the student object seem to get lost. I checked that all the student object work just fine. I think the problem lies at the @property in my student class.

Any comments and suggestions are appreciated

This is an excerpt of student.h As said only the string values get lost, the int (here the plz value) remains correct

@property (nonatomic, weak)NSString *lastname;
@property (nonatomic, weak)NSString *surname;
@property (nonatomic, weak)NSString *street;
@property (nonatomic, assign)int plz;

EDIT: Here is where i parse my json.

for (NSDictionary *dic in jsonArray){

        NSNumber *identity = [dic valueForKey:@"id"] ;
        NSString *firstName = (NSString*) [dic valueForKey:@"first_name"];
        NSString *lastName = (NSString*) [dic valueForKey:@"last_name"];
        NSString *street = (NSString*) [dic valueForKey:@"street"];
        NSNumber *plz = [dic valueForKey:@"plz"] ;
        NSString *birth = (NSString*) [dic valueForKey:@"date_of_birth"];
        NSArray *bills = dic[@"bills"];
        NSArray *hours = dic[@"hours"];

        NSLog(@"First %@",firstName);



        Student *student = [[Student alloc]initWithLastName:lastName withSurname:firstName withStreet:street withPLZ:plz withOrt:@"Uitikon" withBirthDate:birth withOccupation:@"Schüler"];

        [ApprenticeList addObject:student];
    }

EDIT 2 :

I found out that the string values get lost even before the segue. All these objects are created in

ViewDidLoad

But in

prepareforsegue

all the values are allready null (except for the int) .So the only place where the student objects work is in

ViewdidLoad
Hadjimina
  • 35
  • 8
  • 3
    Strings, sets dictionaries and arrays have mutable and immutable versions. It's a good idea to use copy instead of strong for such properties in most cases. The question you should be asking yourself here is "why use weak"? – Abizern Mar 08 '15 at 22:31
  • What does you init method look like? – Sunkas Mar 09 '15 at 07:22
  • Are you sure your strings are non-nil after setting them with [dic valueForKey:@"first_name"]; ? – Sunkas Mar 09 '15 at 07:39
  • @Abizen I really did not know better. I did it because i have always done it this way and never had problems before. My mistake that I did not do enough research . – Hadjimina Mar 09 '15 at 10:38
  • @Sunkas yes, the object get created correctly and I can call all my different methods of the student object on it and it works just fine. – Hadjimina Mar 09 '15 at 10:40

1 Answers1

0
@property (nonatomic, weak)NSString *lastname;
@property (nonatomic, weak)NSString *surname;
@property (nonatomic, weak)NSString *street;

change to

@property (nonatomic, strong)NSString *lastname;
@property (nonatomic, strong)NSString *surname;
@property (nonatomic, strong)NSString *street;

You can also use 'copy'. It will cause the setter for that property to create a copy of the object, otherwise it is identical to strong.

Adeel Ahmad
  • 939
  • 1
  • 10
  • 20
  • Sadly it is still not working. I have test it in here : `- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { NSLog(@"THIS IS IT %@", [[ApprenticeList objectAtIndex:1]getFullName]);` and it is still null. This prepare and the parsing are on both in the same class. (Studentlist.m/h) – Hadjimina Mar 09 '15 at 13:53
  • strange, try checking your student init code again. if your are successfully able to parse the json, have firstname in the NSLog statement, then init is the only place left to check – Adeel Ahmad Mar 10 '15 at 07:48