方法调用中的 NSBundle 参数

发布于 2024-10-20 10:40:23 字数 524 浏览 4 评论 0原文

我必须调用这个方法:

[super initWithPNGFileName: "/Volumes/SAGITTER/Lavoro/Progetti xCode/IVBricker test/ball.png" andGame:game andCGRect:CGRectMake(x,y,16,16)];

所以当我把它放在iPhone上时,路径不对。我知道我必须使用 NSBundle,但如果我这样做,

[super initWithPNGFileName:[[NSBundle mainBundle] pathForResource:@"ball" ofType:@"png" inDirectory:@"/"] andGame:game andCGRect:CGRectMake(x,y,16,16)];

我会收到警报:“从不兼容的指针类型传递 'initWithPNGFileName:andGame:andCGRect:' 的参数 1”

我应该如何继续使用 NSBundle?

I have to call this method:

[super initWithPNGFileName: "/Volumes/SAGITTER/Lavoro/Progetti xCode/IVBricker test/ball.png" andGame:game andCGRect:CGRectMake(x,y,16,16)];

So when I put this on the iPhone, the path is not right. I know I must use NSBundle but if I do

[super initWithPNGFileName:[[NSBundle mainBundle] pathForResource:@"ball" ofType:@"png" inDirectory:@"/"] andGame:game andCGRect:CGRectMake(x,y,16,16)];

I get the alert: "passing argument 1 of 'initWithPNGFileName:andGame:andCGRect:' from incompatible pointer type"

How should I proceed using NSBundle?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帅气称霸 2024-10-27 10:40:23

if [super initWithPNGFileName: "/Volum... 是 100% 正确的代码,我认为此消息需要 C 风格字符串。
但是 [[NSBundle mainBundle] pathForResource:...] 将返回一个 NSString 对象。

尝试将 [[[NSBundle mainBundle] pathForResource:@"ball" ofType:@"png" inDirectory:@"/"] cStringUsingEncoding:NSUTF8StringEncoding] 传递给该方法。

if [super initWithPNGFileName: "/Volum... is 100% correct code I think this message expects a C-Style string.
But [[NSBundle mainBundle] pathForResource:...] will return a NSString object.

Try to pass [[[NSBundle mainBundle] pathForResource:@"ball" ofType:@"png" inDirectory:@"/"] cStringUsingEncoding:NSUTF8StringEncoding] to the method.

北方的韩爷 2024-10-27 10:40:23

您的第一个调用将 c 字符串传递给 initWithPNGFileName: 方法,而 -pathForResource: 返回 NSString。从从捆绑包获得的路径中获取 c-string 并将该字符串传递给您的方法:

[super initWithPNGFileName:[[[NSBundle mainBundle] pathForResource:@"ball" ofType:@"png" inDirectory:@"/"] UTF8String]
                   andGame:game 
                 andCGRect:CGRectMake(x,y,16,16)];

Your 1st call passes c-string to initWithPNGFileName: method, while -pathForResource: returns NSString. Get c-string from the path you get from bundle and pass that string to your method:

[super initWithPNGFileName:[[[NSBundle mainBundle] pathForResource:@"ball" ofType:@"png" inDirectory:@"/"] UTF8String]
                   andGame:game 
                 andCGRect:CGRectMake(x,y,16,16)];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文