我已将所有类别文件和标头包含复制到新的 Xcode 项目中,并将 DataViewControler 中的一段代码粘贴到 Tab 的视图控制器之一(SecondViewController);都在 viewDidLoad() 部分内。
这是复制和粘贴代码的片段:
第二视图控制器.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;
}
@结尾
每当我尝试使用 myObject 调用类别函数时,应用程序就会在包含该代码的页面上崩溃(在这种情况下,当我切换到包含电池信息的选项卡时)。
否则没有警告或错误,它只是崩溃。
下面是日志。有没有人经历过这样的事情?可能我在影响这些东西的过程的早期更新了原始设置中的一些设置,然后我就忘记了它。
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)