0

tearing my hair out here I need to put the values of into an array as a string someone on here advised me to set up like this...

[twitterLocation addObject:[NSString stringWithFormat:@"%g,%g", latitude, longitude]];

However this is not working. The latitude, longitude contain values in my log but will not combine into the string and the array - IT GET RETURNED AS NULL. MY latitude, longitude are set as DOUBLES Thanks

Alan
  • 63
  • 7

1 Answers1

1

Form reading the comments I guess you need to at least allocate your array:

double lat = 0.3231231;
double lon = 0.4343242;
NSMutableArray *twitterLocation = [[NSMutableArray alloc]init];
[twitterLocation addObject:[NSString stringWithFormat:@"%g%g", lat, lon]];

If you skip the allocation the mutable array has no memory location to hold objects.

Luc Wollants
  • 880
  • 9
  • 27
  • I though that setting up pointers in the .h and synthesising it did that? – Alan Jan 21 '13 at 22:30
  • 1
    The @synthesize directive automatically generates the setters and getters. But no memory is allocated, that is still your job. Because the compiler can make no assumption on which init call you will use. In this case we did use init, but it would have been possible to use initWithObjecs – Luc Wollants Jan 21 '13 at 22:32
  • Thanks Luc and Rob - its amazing, just when you think after a few months you have cracked something you end up questioning your earlier understandings / learnings. I still cant get my head around that as there are lots of pointers I have created and synthesised without allocating memory - and they work?! – Alan Jan 21 '13 at 22:36
  • Yes, those pointers can work without an alloc and init if they point to something like an NSString, NSNumber, etc... One extra rule of thumb in Objective-C is to check the value of your property before sending it a message. In your case twitterLocation would have been nil. Sending to nil is valid but results in nothing being done. So the message addObject result in no operation. This is different in languages like C# and Java. A must read for every new developer is http://tinyurl.com/a3fjuyt and http://tinyurl.com/ayervl3 – Luc Wollants Jan 22 '13 at 18:19