为什么传递 NSString 指针对我不起作用?
我在通过函数调用传递/接收 NSString 指针时遇到问题。我希望有人能帮助我看看我做错了什么。
这是我的第一堂课...
void addTo(int pk, NSString* nam, NSString *descrip)
{
//open the database
sqlite3 *db;
db = [Item openDB:databasePath];
printf("'%i', '%s', '%s'", pk, nam, descrip);
//create new item with key, name, description, and database
Item *Obj = [[Item alloc]initWithPrimaryKey:pk:nam:descrip:db];
.
.
.
}
然后这是 Item.m 中的函数,如上所述调用...
- (id) initWithPrimaryKey:(NSInteger) pk :(NSString*) nam: (NSString*) descrip: (sqlite3*) db{
printf("'%i', '%s', '%s'", pk, nam, descrip);
.
.
.
return self;
}
假设我使用输入 1234、“Tree”、“plant with leaves”调用 addTo
第一个代码中的打印块输出我发送到 addTo 的内容,但 initWithPrimaryKey 中的打印打印以下内容...
'1234', 'P?a', 'P?a'
这是为什么?或者更多......为什么它没有打印出我期望的内容?
I am having troubles passing/receiving NSString pointers through function calls. I'm hoping someone can help me see what I'm doing incorrectly.
So this is from my first class...
void addTo(int pk, NSString* nam, NSString *descrip)
{
//open the database
sqlite3 *db;
db = [Item openDB:databasePath];
printf("'%i', '%s', '%s'", pk, nam, descrip);
//create new item with key, name, description, and database
Item *Obj = [[Item alloc]initWithPrimaryKey:pk:nam:descrip:db];
.
.
.
}
And then this is the function in Item.m called as above...
- (id) initWithPrimaryKey:(NSInteger) pk :(NSString*) nam: (NSString*) descrip: (sqlite3*) db{
printf("'%i', '%s', '%s'", pk, nam, descrip);
.
.
.
return self;
}
Let's say I call addTo with inputs 1234, "Tree", "plant with leaves"
The print in the first code block outputs what I sent to addTo but the print in initWithPrimaryKey prints the following...
'1234', 'P?a', 'P?a'
Why is this? Or more.. why is it not printing what I expect?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
%s
用于char*
字符串,但%@
应该与 NSString 一起使用。printf
可能支持也可能不支持%@
,我不知道。如果没有,则需要使用NSString的cStringUsingEncoding或UTF8String转换为可以使用的char*
。initWithPrimaryKey:pk:nam:descrip:db
是无效的(或者至少非常循环)语法,顺便说一句,initWithPrimaryKey:(NSInteger) pk :(NSString*) nam: (NSString* ) 描述:(sqlite3*) db
。了解
NSString
和char*
字符串之间的区别非常重要。 NSString 是一个成熟的对象,它支持大约 50 种方法,可以使用字符串值执行各种整洁/奇怪/(偶尔)淫秽的操作。两者无论如何都不能互换。与某些 C++ 库不同,您无法在调用时用char*
字符串替换NSString
并进行自动转换。因此,对字符串使用
"letters"
不会产生可用作 NSString 的内容——您必须使用@"letters"
。%s
is used forchar*
strings, but%@
should be used with NSStrings.printf
may or may not support%@
, I don't know. If not, you need to use NSString's cStringUsingEncoding or UTF8String to convert to achar*
that you can use.initWithPrimaryKey:pk:nam:descrip:db
is invalid (or at least very loopy) syntax, BTW, as isinitWithPrimaryKey:(NSInteger) pk :(NSString*) nam: (NSString*) descrip: (sqlite3*) db
.It's important to understand the difference between an
NSString
and achar*
string. The NSString is a full-fledged object, with about 50 methods that it supports to do all sorts of neat/strange/(and occasionally)obscene things with string values. The two are not in any way interchangeable. And unlike with some C++ libraries, you can't substitute achar*
string for anNSString
on a call and have automatic conversion occur.So using
"letters"
for a string will not produce something usable as an NSString -- you must use@"letters"
.在 NSLog 中打印时,NSString 应该是 %@ 而不是 %s
When printing in the NSLog the NSString should be %@ not %s