【发布时间】:2014-06-14 00:10:05
【问题描述】:
我是 Cocoa 编程的新手,从未接触过 Objective-C。现在,我正在尝试通过阅读 Aaron Hillegrass 的书“Cocoa Programming for Mac OS X, 4e”来学习 Swift,并在 Swift 而不是 Obj-C 中实现所有内容。到目前为止一切顺利,但我成功了第 8 章中的障碍(RaiseMan 应用程序)。
这是书中的 Objective-C 代码:
标题:
#import <Foundation/Foundation.h>
@interface Person : NSObject {
NSString *personName;
float expectedRaise;
}
@property (readwrite, copy) NSString *personName;
@property (readwrite) float expectedRaise;
@end
这是实现
#import "Person.h"
@implementation Person
@synthesize personName;
@synthesize expectedRaise;
- (id)init
{
self = [super init];
if (self) {
expectedRaise = 0.05;
personName = @"New Person";
}
return self;
}
@end
然后你应该去 Interface Builder,获取一个数组控制器,指定 Person 作为它的类,并添加 personName 和 expectedRaise 作为它的键。
我在 Swift 中重写了 Person 类如下:
import Cocoa
class Person: NSObject {
var personName = String()
var expectedRaise = Float()
}
并按照书中的说明将其连接到 ArrayController。
Document文件中也有一些代码:
init() {
employees = NSMutableArray()
println("hi")
super.init()
// Add your subclass-specific initialization here.
var p = Person()
p.personName = "New Person"
p.expectedRaise = 0.05
}
这是界面的样子(所以不会让我直接发布)https://www.dropbox.com/s/e5busxes9ex3ejd/Screenshot%202014-06-13%2019.02.37.png
当我尝试运行应用程序并单击“添加员工”按钮时,我在控制台中收到此错误:
“RaiseMan[9290:303] 找不到名为 Person 的对象类”
所以,我的问题是:我做错了什么?
【问题讨论】:
-
+1 因为和我有完全相同的问题!从 2008 年开始,我以为我用这本书在 Swift 中编写 Cocoa 应用程序是疯了。我知道我不是唯一一个以艰难的方式学习好东西的人。