问题描述
我写了一个名为SomeValues的单例,其中我初始化一个foo NSMutableArray。然后我尝试写一个函数SetBFSV从不同的控制视图设置此数组的值。
@interface SomeValues:NSObject {
NSMutableArray * foo;}
+(SomeValues *)sharedInstance;
@implementation
...
- (void)SetBFSV:(char)lbl ToVal:(long)BFSvl {
NSNumber * ValueBFSvl = [NSNumber numberWithLong:BFSvl];
NSString * Strlbl = [[NSString alloc] stringWithFormat:@%s,lbl];
[foo setValue:ValueBFSvl forKey:Strlbl];
}
我知道setValue需要一个NS对象的值和键,但我不能将我的函数声明为
(void)SetBFSV:(NSString)lbl ToVal:(NSNumber)BFSvl
因为它不编译时出现错误:无法使用对象作为方法的参数 p>
在一个ControlView中我写了这段代码:
SomeValues * myBFSV = [SomeValues sharedInstance];
const unsigned char * Bar =(unsigned char *)[@Label1UTF8String];
NSLog(@字符串%s,Bar);
[myBFSV SetBFSV:Bar ToVal:2.5];
在编译时,我在最后一行得到警告:
警告:SetBFSV:ToVal:的传递参数1从无指针的指针中获取整数
我越来越愚蠢的环顾四周。当运行我从NSLog打印输出,但紧接着程序显然崩溃与此错误:
'NSInvalidArgumentException',reason:' - [NSPlaceholderString stringWithFormat:] :无法识别的选择器发送到实例0x4e03280'
显然,我向stringWithFormat传递错误,但我不明白。
感谢任何帮助。有一个愉快的一天!
/ luca
您不能使用对象作为参数:
(void)SetBFSV:(NSString)lbl ToVal:(NSNumber)BFSvl
您需要使用星号作为指针传递。
(void)SetBFSV:(NSString *)lbl ToVal:(NSNumber *)BFSvl
此外,如果你需要传递
(char)
和(long)
您的位代码:SomeValues * myBFSV = [SomeValues sharedInstance];
const unsigned char * Bar =(unsigned char *)[@Label1UTF8String];
NSLog(@字符串%s,Bar);
[myBFSV SetBFSV:Bar ToVal:2.5]; //在任何一种情况下,这是一个长。你为什么在这里传递一个双?
以指针形式传递
* Bar
你应该真正阅读指针和对象。如果你需要以(const char *)
和(long)
的方式传递它们, p>
- (void)SetBFSV:(const char *)lbl ToVal:(long)BFSvl {
NSNumber * ValueBFSvl = NSNumber numberWithLong:BFSvl];
NSString * Strlbl = [NSString stringWithUTF8String:lbl]; //你的旧代码在这里有一个内存泄漏。你需要创建一个autorelease对象,或者在添加到`foo`后释放它
[foo setValue:ValueBFSvl forKey:Strlbl];
}
我的建议是执行以下操作:
- (void)SetBFSV:(NSString *)key ToVal:(long)val {
NSNumber * value = [NSNumber numberWithLong:val];
[foo setValue:value forKey:key];
}
并像下面这样调用(从你的例子):
SomeValues * myBFSV = [SomeValues sharedInstance];
NSString * Bar = [NSString stringWithString:@Label1]; //这可能不是必须的...
NSLog(@字符串%@,Bar);
[myBFSV SetBFSV:Bar ToVal:25]; //不传递小数(长)
I wrote a singleton called SomeValues where I initialize a foo NSMutableArray. I then tried to write a function SetBFSV to set the values of this array from different control views.
@interface SomeValues : NSObject { NSMutableArray *foo;} + (SomeValues *) sharedInstance; @implementation ... - (void) SetBFSV:(char)lbl ToVal:(long)BFSvl{ NSNumber *ValueBFSvl = [NSNumber numberWithLong:BFSvl]; NSString *Strlbl = [[NSString alloc] stringWithFormat:@"%s",lbl]; [foo setValue:ValueBFSvl forKey:Strlbl]; }
I know that setValue requires a NS object for both the value and the key, but I cannot declare my function as
(void) SetBFSV:(NSString)lbl ToVal:(NSNumber)BFSvl
because it doesn't compile with the error: "Can not use an object as parameter to a method".
In one ControlView I wrote then this piece of code:
SomeValues *myBFSV = [SomeValues sharedInstance]; const unsigned char *Bar = (unsigned char *)[@"Label1" UTF8String]; NSLog(@"The string %s", Bar); [myBFSV SetBFSV:Bar ToVal:2.5];
When compiling I get a warning on the last line:
warning: passing argument 1 of 'SetBFSV:ToVal:' makes integer from pointer without a cast
Which integer? I'm getting stupid looking around for it. When running I get the print out from the NSLog, but right afterwards the program obviously crashes with this error:
'NSInvalidArgumentException', reason: '-[NSPlaceholderString stringWithFormat:]: unrecognized selector sent to instance 0x4e03280'
Clearly I'm passing something wrong to stringWithFormat but I cannot understand what.
Thanks for any help. Have a nice day!
/luca
解决方案For one, you can't use an object as a paramter:
(void) SetBFSV:(NSString)lbl ToVal:(NSNumber)BFSvl
You need to pass them as pointers, using the asterisk.
(void) SetBFSV:(NSString*)lbl ToVal:(NSNumber*)BFSvl
Furthermore, if you need to pass them as
(char)
and(long)
your bit of code:SomeValues *myBFSV = [SomeValues sharedInstance]; const unsigned char *Bar = (unsigned char *)[@"Label1" UTF8String]; NSLog(@"The string %s", Bar); [myBFSV SetBFSV:Bar ToVal:2.5]; // in either case, this is a long. why are you passing a double here?
Passes
*Bar
as a pointer. You should really read up on the pointers and objects. IF you need to pass them in as(const char*)
and(long)
do it like this:- (void) SetBFSV:(const char*)lbl ToVal:(long)BFSvl{ NSNumber *ValueBFSvl = [NSNumber numberWithLong:BFSvl]; NSString *Strlbl = [NSString stringWithUTF8String: lbl]; // your old code had a memory leak here. you need to either create an autorelease object, or release it after adding to `foo` [foo setValue:ValueBFSvl forKey:Strlbl]; }
My Recommendation is to do the following:
- (void) SetBFSV:(NSString*)key ToVal:(long)val{ NSNumber *value = [NSNumber numberWithLong:val]; [foo setValue:value forKey:key]; }
And call it like the following (from your example):
SomeValues *myBFSV = [SomeValues sharedInstance]; NSString *Bar = [NSString stringWithString:@"Label1"];// this may not be necessary... NSLog(@"The string %@", Bar); [myBFSV SetBFSV:Bar ToVal:25]; // don't pass a decimal for a (long)
这篇关于目标c:传递一个char参数的函数会崩溃的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!