【发布时间】:2016-01-13 23:11:37
【问题描述】:
我有一个名为Chord 的类,它是UILabel 的子类:
import UIKit
class Chord: UILabel {
var numTextLine: Int?
var positionInTextLine: CGFloat?
var tempPosInLine: CGFloat?
init(chordName: String, dLine: DLine, xAxis: CGFloat, posInTextLine: CGFloat) {
// posInLine: CGFloat
let labelSize = chordName.sizeWithAttributes([NSFontAttributeName: UIFont.systemFontOfSize(14.0)])
let labelPositionY = ((dLine.upLine.frame.height) - labelSize.height) / 2
super.init(frame: CGRect(origin: CGPoint(x: xAxis, y: labelPositionY), size: labelSize))
self.numTextLine = dLine.numTextLine
self.positionInTextLine = posInTextLine
self.tempPosInLine = self.positionInTextLine
self.text = chordName
self.font = self.font.fontWithSize(14)
self.textAlignment = NSTextAlignment.Center
self.userInteractionEnabled = true
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
//fatalError("init(coder:) has not been implemented")
}
}
我想使用 CoreData 持久存储一个名为 Song 的对象。 Song 对象的属性是Chord 的数组。所以我将NSManagedObject 子分类如下:
import Foundation
import CoreData
class Song: NSManagedObject {
@NSManaged var lyrics: NSString?
@NSManaged var chords: NSData?
@NSManaged var title: NSString?
@NSManaged var artist: NSString?
}
基本上我被困在那里。我已在 CoreData 模型视图中将属性的类型设置为 BinaryData 并尝试在保存 ManagedContext 之前使用 NSKeyedArchiver.archivedDataWithRootObject(myObject) 在获取实体后使用 NSKeyedArchiver.unarchiveObjectWithData(myObject) as? [Chord],但是当我取消归档 NSData 时,我得到了一个 UILabel 对象虽然我已经设置了as? [Chord]。
任何人都可以让我走上正确的道路吗?
【问题讨论】: