我正在将一个应用程序从Objective-C切换到Swift,我有几个带有存储属性的类别,例如:
@interface UIView (MyCategory)
- (void)alignToView:(UIView *)view
alignment:(UIViewRelativeAlignment)alignment;
- (UIView *)clone;
@property (strong) PFObject *xo;
@property (nonatomic) BOOL isAnimating;
@end
Run Code Online (Sandbox Code Playgroud)
由于Swift扩展不接受这些存储的属性,我不知道如何维护与Objc代码相同的结构.存储的属性对我的应用程序非常重要,我相信Apple必须在Swift中创建一些解决方案.
正如jou所说,我所寻找的实际上是使用关联对象,所以我做了(在另一个上下文中):
import Foundation
import QuartzCore
import ObjectiveC
extension CALayer {
var shapeLayer: CAShapeLayer? {
get {
return objc_getAssociatedObject(self, "shapeLayer") as? CAShapeLayer
}
set(newValue) {
objc_setAssociatedObject(self, "shapeLayer", newValue, UInt(OBJC_ASSOCIATION_RETAIN))
}
}
var initialPath: CGPathRef! {
get {
return objc_getAssociatedObject(self, "initialPath") as CGPathRef
}
set {
objc_setAssociatedObject(self, "initialPath", newValue, UInt(OBJC_ASSOCIATION_RETAIN))
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是在执行以下操作时我得到了EXC_BAD_ACCESS:
class UIBubble : UIView {
required init(coder …Run Code Online (Sandbox Code Playgroud) 当使用关联对象,从iOS 4和OSX 10.6开始提供Objective-C运行时功能时,必须定义用于在运行时存储和检索对象的密钥.
典型的用法是定义如下的密钥
static char const * const ObjectTagKey = "ObjectTag";
Run Code Online (Sandbox Code Playgroud)
然后使用是存储对象
objc_setAssociatedObject(self, ObjectTagKey, newObjectTag, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
Run Code Online (Sandbox Code Playgroud)
并检索它
objc_getAssociatedObject(self, ObjectTagKey);
Run Code Online (Sandbox Code Playgroud)
(例如http://oleb.net/blog/2011/05/faking-ivars-in-objc-categories-with-associative-references/)
是否有更简洁的方法来定义关联的对象键,这不涉及额外变量的声明?
关于swift枚举的一般问题.
我想创建一个"icon"的枚举,并将一个值"关联"到枚举的情况
enum Icon {
case plane
case arrow
case logo
case flag
}
Run Code Online (Sandbox Code Playgroud)
我想为枚举值创建一个关联的图像.还有枚举值的相关颜色
因此,例如,如果有可能做类似的事情:
extension Icon.plane {
var image = {
get {
return UIImage("plane.png")
}
}
var color = {
get {
return UIColor.greenColor()
}
}
}
var image = Icon.arrow.image // the image associated to the enum
var color = Icon.arrow.color // the color associated to the enum
Run Code Online (Sandbox Code Playgroud)
这类事可能吗?
enums associated-types computed-values associated-object swift
我知道在类上声明私有属性的可能性是通过将它们放在该类的implementation(.m)文件中声明的该类的未命名类别中.那不是我想要做的.
我正在处理一个类的命名类别,该类为该类添加了一些功能.对于这个功能,在我的类别中使用私有属性对我非常有帮助 - 所以通常的实现方法(如上所述)似乎对我不起作用.或者是吗?请赐教!
properties private objective-c objective-c-category associated-object
我factory.LazyAttribute在SubFactory调用中使用传入一个对象,在对象中创建factory_parent.这很好用.
但是,如果我将创建的对象传递给a RelatedFactory,LazyAttribute则无法再看到factory_parent并失败.
这很好用:
class OKFactory(factory.DjangoModelFactory):
class = Meta:
model = Foo
exclude = ['sub_object']
sub_object = factory.SubFactory(SubObjectFactory)
object = factory.SubFactory(ObjectFactory,
sub_object=factory.LazyAttribute(lambda obj: obj.factory_parent.sub_object))
Run Code Online (Sandbox Code Playgroud)
同样的呼叫在LazyAttribute这里失败:
class ProblemFactory(OKFactory):
class = Meta:
model = Foo
exclude = ['sub_object', 'object']
sub_object = factory.SubFactory(SubObjectFactory)
object = factory.SubFactory(ObjectFactory,
sub_object=factory.LazyAttribute(lambda obj: obj.factory_parent.sub_object))
another_object = factory.RelatedFactory(AnotherObjectFactory, 'foo', object=object)
Run Code Online (Sandbox Code Playgroud)
相同的LazyAttribute调用不能再看到factory_parent,并且只能访问AnotherObject值.LazyAttribute抛出错误:
AttributeError: The parameter sub_object is unknown. Evaluated attributes are...[then lists all …Run Code Online (Sandbox Code Playgroud) 我工作于Django 2.0
我有一个Note用于保存笔记的模型,还有另外两个模型用于向笔记添加颜色标签。
class Note(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=250, blank=True, default='Untitled')
content = models.TextField(blank=True)
class ColorLabels(models.Model):
title = models.CharField(max_length=100, unique=True)
value = models.CharField(max_length=100)
default = models.BooleanField(default=False)
class NoteLabel(models.Model):
note = models.OneToOneField(Note, on_delete=models.CASCADE)
color_label = models.OneToOneField(ColorLabels, on_delete=models.CASCADE)
Run Code Online (Sandbox Code Playgroud)
与对象Note
note = Note.objects.get(pk=1)
Run Code Online (Sandbox Code Playgroud)
我想访问关联的ColorLabels和title字段value或NoteLabel对象。
因为它们是一对一的字段。我尝试做
note.note_label
note.NoteLabel
note.note_label_set
Run Code Online (Sandbox Code Playgroud)
但所有返回错误为
AttributeError: 'Note' object has no attribute 'note_label_set'
Run Code Online (Sandbox Code Playgroud) 我正在学习 cakePHP 3.0 并且在我的模型上保存相关数据时遇到了一些问题。
我尝试使用 ClientPreferences 的关联数据保存客户端
客户表
class ClientsTable extends Table
{
public function initialize(array $config)
{
(...)
$this->belongsTo('ClientPreferences', [
'foreignKey' => 'client_preferences_id'
]);
}
}
Run Code Online (Sandbox Code Playgroud)
客户端控制器
$aClient = $this->Clients->newEntity();
$aClient = $this->Clients->patchEntity($aClient, $this->request->data);
$aClientPreference = $this->Clients->ClientPreferences->newEntity();
$aClientPreference->my_field = 'my value';
$aClient->ClientPreferences = $aClientPreference;
$this->Clients->save($aClient, ['associated' => ['ClientPreferences']];
Run Code Online (Sandbox Code Playgroud)
Client 实体已正确保存,但关联的 ClientPreferences 实体未正确保存,并且 Cake 没有抛出错误。
我试图遵循这个:http : //book.cakephp.org/3.0/en/orm/saving-data.html# Saving-with-associations
但还没有发现任何问题来正确地做。有人有什么建议吗?
先感谢您。
cakephp save model-associations associated-object cakephp-3.0
我试图将属性关联到数组扩展:
private var AssociatedObjectHandle: String = "BlaBLabla"
extension Array {
var emptyIndex:Int {
mutating get {
if let object = objc_getAssociatedObject(self, &AssociatedObjectHandle) {
return object as! Int
}
let index = self.searchEmptyIndex()
self.emptyIndex = index
return index
}
set {
let new = (newValue as NSInteger)
objc_setAssociatedObject(self, &AssociatedObjectHandle, new, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
}
}
func searchEmptyIndex() -> Int {
if let arr = self as? [Int] {
return arr.index(of: -1)!
}
return -1
}
}
Run Code Online (Sandbox Code Playgroud)
objc_getAssociatedObject调用总是返回nil !!
任何人都知道为什么?
关于这个,我正在敲打最后一小时...
swift ×3
django ×2
objective-c ×2
python ×2
cakephp ×1
cakephp-3.0 ×1
enums ×1
factory-boy ×1
ios ×1
one-to-one ×1
private ×1
properties ×1
save ×1