I’ve copied all my category files & header includes to the new Xcode project, and pasted a section of code that was in the DataViewControler to one of the Tab’s view controllers (SecondViewController); both inside the viewDidLoad() section.
Here’s a snippet of the copy & pasted code:
SecondViewController.m
#import "SystemStatusReportSecondViewController.h"
#import "memAndProcesses1.h"
#include <stddef.h>
#import "NSObject+BatteryStats.h"
#import "NSObject+DeviceInfo.h"
#import "IPAddress.h"
@interface SystemStatusReportSecondViewController ()
@end
@implementation SystemStatusReportSecondViewController
@synthesize batteryTextView;
- (void)viewDidLoad
{
[super viewDidLoad];
NSObject *myObject = [[NSObject alloc] init];
[myObject autorelease];
UIDevice *device = [UIDevice currentDevice];
//...
NSString *batteryStatus = [myObject simpleBatteryStatus];
}
BatteryStats.m
#import "NSObject+BatteryStats.h"
@implementation NSObject (BatteryStats)
- (float)batteryLevel {
UIDevice *myDevice = [UIDevice currentDevice];
[myDevice setBatteryMonitoringEnabled:YES];
float batteryLevel = [myDevice batteryLevel];
return batteryLevel;
}
- (NSString*)simpleBatteryStatus {
NSArray *batteryStatus = [NSArray arrayWithObjects:
@"Battery status is unknown.",
@"Battery is in use (discharging).",
@"Battery is charging.",
@"Battery is fully charged.", nil];
int batteryState = [[UIDevice currentDevice] batteryState];
NSString *batteryChargeStr = [batteryStatus objectAtIndex:batteryState];
return batteryChargeStr;
}
@end
Whenever I try to call a category function with myObject the app crashes on the page that contains that code (in this case, when I switch to the tab with battery information).
There are otherwise no warnings or errors, it just crashes.
Below is the log. Has anyone experienced something like this before? It’s possible I had updated some setting in the original early in the process that affects this stuff and then I just forgot about it.
2012-06-28 13:31:53.965 SystemStatusReport[8269:f803] -[NSObject simpleBatteryStatus]: unrecognized selector sent to instance 0x6840440
2012-06-28 13:31:54.025 SystemStatusReport[8269:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSObject simpleBatteryStatus]: unrecognized selector sent to instance 0x6840440'
*** First throw call stack:
(0x13c8022 0x1559cd6 0x13c9cbd 0x132eed0 0x132ecb2 0x301b 0xd9a1e 0xf50a9 0xf4edd 0xf34aa 0xf334f 0xf4e14 0x13c9e99 0x1514e 0x150e6 0x23d4bd 0x13c9e99 0x1514e 0x150e6 0xbbade 0xbbfa7 0xbbb13 0x23fc48 0x13c9e99 0x1514e 0x150e6 0xbbade 0xbbfa7 0xbb266 0x3a3c0 0x3a5e6 0x20dc4 0x14634 0x12b2ef5 0x139c195 0x1300ff2 0x12ff8da 0x12fed84 0x12fec9b 0x12b17d8 0x12b188a 0x12626 0x2a12 0x2985 0x1)
terminate called throwing an exception(lldb)
There are 2 possibilities:
#include BatteryStats.hin yourSecondViewController.m, or-simpleBatteryStatusmethod in your category there’s only one method in the code you posted, the-batteryLevelTip: If you want your categories to be loaded automatically, add
-ObjCflag to your target (in Target > Build Settings > Other Linker Flags).