[XCODE] Calculating distances and adding them into an array

Socius Decennalis
Decennium
Joined
Aug 15, 2008
Messages
489
Reaction score
43
I'm creating an iOS application that checks users current location and then calculates distance between set points.

I've managed to create a code that calculates the distance.
Code:
NSMutableArray * distances = [[NSMutableArray alloc] init];

    CLLocation *location1 = [[CLLocation alloc] initWithLatitude:mapView.userLocation.location.coordinate.latitude longitude:mapView.userLocation.location.coordinate.longitude]; // User
    CLLocation *location2 = [[CLLocation alloc] initWithLatitude:VILLAPAULI_LATITUDE longitude:VILLAPAULI_LONGITUDE]; // Location

NSLog(@"Distance is %f kilometers.", [location1 distanceFromLocation:location2] / 1000);

[distances addObject:@"%f", [location1 distanceFromLocation:location2]];
But the last line of code gives an error "Too many arguements to method call, expected 1, have 2".
 
Last edited:
Well as the error message says, you're passing in two arguments instead of one. I know you're trying to just add a string from the looks of it, but what you're doing is adding in a string of "%f" and also an object [location1 ...]

What you want to do is first create the string then add it to the array. (Or you could do this inline as well)

Code:
NSString *myString = [NSString localizedStringWithFormat:@"%f", [location1 distanceFromLocation:location2]];
[distances addObject:myString];
 
Well as the error message says, you're passing in two arguments instead of one. I know you're trying to just add a string from the looks of it, but what you're doing is adding in a string of "%f" and also an object [location1 ...]

What you want to do is first create the string then add it to the array. (Or you could do this inline as well)

Code:
NSString *myString = [NSString localizedStringWithFormat:@"%f", [location1 distanceFromLocation:location2]];
[distances addObject:myString];
Thank you. This works just as I wanted.

How can I use this array in another class, though? I have been googling around, but none of it seems to work properly or I am just implementing it wrongly.

I added to my MapViewController.h
Code:
@property (nonatomic, retain) NSMutableArray *distances;
And changed from MapViewController.m NSMutableArray *distances = [[NSMutableArray alloc] init]; to distances = [[NSMutableArray alloc] init];

Then I added to TableViewController.m
Code:
    MapViewController *TableViewController = [[MapViewController alloc]initWithNibName:@"MapViewController" bundle:nil];
    TableViewController.distances = tableSubtitles;
    
    tableSubtitles = [[NSMutableArray alloc] init];

But whenever I launch the application and go to the table view, I get this error.
Code:
2013-12-11 15:01:34.809 MapApp[2163:a0b] Unknown class UITableViewCellStyleSubtitle in Interface Builder file.
2013-12-11 15:01:34.815 MapApp[2163:a0b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
*** First throw call stack:
(
	0   CoreFoundation                      0x019105e4 __exceptionPreprocess + 180
	1   libobjc.A.dylib                     0x016938b6 objc_exception_throw + 44
	2   CoreFoundation                      0x018b14e6 -[__NSArrayM objectAtIndex:] + 246
	3   MapApp                         0x000083bb -[TableViewController tableView:cellForRowAtIndexPath:] + 523
	4   UIKit                               0x0039961f -[UITableView _createPreparedCellForGlobalRow:withIndexPath:] + 412
	5   UIKit                               0x003996f3 -[UITableView _createPreparedCellForGlobalRow:] + 69
	6   UIKit                               0x0037d774 -[UITableView _updateVisibleCellsNow:] + 2378
	7   UIKit                               0x00390e95 -[UITableView layoutSubviews] + 213
	8   UIKit                               0x00315267 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
	9   libobjc.A.dylib                     0x016a581f -[NSObject performSelector:withObject:] + 70
	10  QuartzCore                          0x047f32ea -[CALayer layoutSublayers] + 148
	11  QuartzCore                          0x047e70d4 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
	12  QuartzCore                          0x047f5715 -[CALayer(CALayerPrivate) layoutBelowIfNeeded] + 43
	13  UIKit                               0x00307c76 -[UIView(Hierarchy) layoutBelowIfNeeded] + 595
	14  UIKit                               0x00307a1d -[UIView(Hierarchy) layoutIfNeeded] + 74
	15  UIKit                               0x003ee07f -[UITabBarController _layoutViewController:] + 220
	16  UIKit                               0x003ee1f2 -[UITabBarController _wrapperViewForViewController:] + 248
	17  UIKit                               0x003f53d0 -[UITabBarController transitionFromViewController:toViewController:transition:shouldSetSelected:] + 441
	18  UIKit                               0x003f4bd2 -[UITabBarController transitionFromViewController:toViewController:] + 63
	19  UIKit                               0x003f0fbb -[UITabBarController _setSelectedViewController:] + 279
	20  UIKit                               0x003f0e9c -[UITabBarController setSelectedViewController:] + 180
	21  UIKit                               0x003f4ace -[UITabBarController _tabBarItemClicked:] + 312
	22  libobjc.A.dylib                     0x016a5874 -[NSObject performSelector:withObject:withObject:] + 77
	23  UIKit                               0x002ac0c2 -[UIApplication sendAction:to:from:forEvent:] + 108
	24  UIKit                               0x002ac04e -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
	25  UIKit                               0x0055a659 -[UITabBar _sendAction:withEvent:] + 479
	26  libobjc.A.dylib                     0x016a581f -[NSObject performSelector:withObject:] + 70
	27  UIKit                               0x002ac0c2 -[UIApplication sendAction:to:from:forEvent:] + 108
	28  UIKit                               0x002ac04e -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
	29  UIKit                               0x003a40c1 -[UIControl sendAction:to:forEvent:] + 66
	30  UIKit                               0x003a4484 -[UIControl _sendActionsForEvents:withEvent:] + 577
	31  UIKit                               0x003a40f6 -[UIControl sendActionsForControlEvents:] + 48
	32  UIKit                               0x0055ef80 -[UITabBar(Static) _buttonUp:] + 123
	33  libobjc.A.dylib                     0x016a5874 -[NSObject performSelector:withObject:withObject:] + 77
	34  UIKit                               0x002ac0c2 -[UIApplication sendAction:to:from:forEvent:] + 108
	35  UIKit                               0x002ac04e -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
	36  UIKit                               0x003a40c1 -[UIControl sendAction:to:forEvent:] + 66
	37  UIKit                               0x003a4484 -[UIControl _sendActionsForEvents:withEvent:] + 577
	38  UIKit                               0x003a3733 -[UIControl touchesEnded:withEvent:] + 641
	39  UIKit                               0x002e951d -[UIWindow _sendTouchesForEvent:] + 852
	40  UIKit                               0x002ea184 -[UIWindow sendEvent:] + 1232
	41  UIKit                               0x002bde86 -[UIApplication sendEvent:] + 242
	42  UIKit                               0x002a818f _UIApplicationHandleEventQueue + 11421
	43  CoreFoundation                      0x0189983f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
	44  CoreFoundation                      0x018991cb __CFRunLoopDoSources0 + 235
	45  CoreFoundation                      0x018b629e __CFRunLoopRun + 910
	46  CoreFoundation                      0x018b5ac3 CFRunLoopRunSpecific + 467
	47  CoreFoundation                      0x018b58db CFRunLoopRunInMode + 123
	48  GraphicsServices                    0x035919e2 GSEventRunModal + 192
	49  GraphicsServices                    0x03591809 GSEventRun + 104
	50  UIKit                               0x002aad3b UIApplicationMain + 1225
	51  MapApp                         0x00008b3d main + 141
	52  libdyld.dylib                       0x01f4c725 start + 0
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
 
Last edited:
The error message: *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'

Is telling you that you are trying to index into an empty array, which the TableViewController is probably trying to do. So in the classes constructor make sure you initialize the array and whenever you try to access an index within that array you should add some assertion checks like: if index >= size of array return;
 
The error message: *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'

Is telling you that you are trying to index into an empty array, which the TableViewController is probably trying to do. So in the classes constructor make sure you initialize the array and whenever you try to access an index within that array you should add some assertion checks like: if index >= size of array return;
EDIT: Nevermind. I solved it by creating a property in AppDelegate.h and adding distances = [[NSMutableArray alloc] init]; to AppDelegate.m's didFinishLaunchingWithOptions.
Now I can access the distances array from any class by adding mainDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; and then referring to it with mainDelegate.distances.

Thanks for all the help.
 
Last edited:
Back