【问题标题】:NSImage initWithData not found?NSImage initWithData 没有找到?
【发布时间】:2012-07-09 13:25:25
【问题描述】:

NSImage class reference中说存在方法initWithData,但是为什么xCode说不存在呢?

我是这样使用的:

    NSData *imgData = [[imageView image] TIFFRepresentation];
fotoImg = [NSImage initWithData:imgData];

我收到警告:

Class method '+initWithData:' not found (return type defaults to 'id')

为什么会这样?

【问题讨论】:

  • [[NSImage alloc] initWithData..

标签: objective-c xcode macos class nsimage


【解决方案1】:

当您应该在该类的实例上调用它时,您正在对该类调用initWithData。这样做:[[NSImage alloc] initWithData:imgData];

了解成员函数和类函数(或 Obj C 术语中的“消息”)之间的区别非常重要。我推荐http://www.amazon.de/Programming-Objective-C-Automatic-Reference-Developers/dp/0321811909/ref=pd_sim_sbs_eb_1/276-1733140-9413556

【讨论】:

  • Objective-C 中没有“静态方式”。它们是类方法和实例方法。通话始终是动态的。
  • 好吧,我更正了。我的意思不是静态而不是动态,但你是对的,这不是正确的术语。
  • 上帝,我遇到了这个问题,但是使用 NSImage initWIthContentsOfFile,你真的为我节省了很多时间。我正在从 iOS 的 [UIImage imageWithContentsOfFile] 转换代码,只是更改了类和方法名称,假设它可以工作。肯定是脑子放屁的那一刻。希望如果有人遇到同样的问题,他们最终会看到此评论。
猜你喜欢
  • 2020-06-14
  • 1970-01-01
  • 2011-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-11
相关资源
最近更新 更多