我已经复制了所有类别文件& header包含到新的Xcode项目中,并将DataViewControler中的一段代码粘贴到Tab的视图控制器之一(SecondViewController);两者都在viewDidLoad()部分内。
以下是该副本的一小部分&粘贴的代码:
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
每当我尝试使用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)
答案 0 :(得分:0)
有两种可能性:
#include BatteryStats.h
或SecondViewController.m
-simpleBatteryStatus
方法,您发布的代码中只有一种方法,-batteryLevel
提示:如果您希望自动加载类别,请将 -ObjC
标记添加到目标(在目标&gt;构建设置&gt;其他链接标记中)
-ObjC加载实现Objective-C类或类别的静态归档库的所有成员。