Questions tagged [associated-object]

60 questions
144
votes
22 answers

How to have stored properties in Swift, the same way I had on Objective-C?

I am switching an application from Objective-C to Swift, which I have a couple of categories with stored properties, for example: @interface UIView (MyCategory) - (void)alignToView:(UIView *)view …
Marcos Duarte
  • 3,472
  • 4
  • 19
  • 22
90
votes
9 answers

Is there a way to set associated objects in Swift?

Coming from Objective-C you can call function objc_setAssociatedObject between 2 objects to have them maintain a reference, which can be handy if at runtime you don't want an object to be destroyed until its reference is removed also. Does Swift…
amleszk
  • 6,192
  • 5
  • 38
  • 43
23
votes
3 answers

Avoid extra static variables for associated objects keys

When using associated objects, an Objective-C runtime feature available starting from iOS 4 and OSX 10.6, it's necessary to define a key for storing and retrieving the object at runtime. The typical usage is defining the key like follows static char…
Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
20
votes
4 answers

Swift - Associated value or extension for an Enum

General question regarding swift enum. I want to create an enum of "icon" and "associate" a value to the enum case enum Icon { case plane case arrow case logo case flag } I want to create an associated image to the enum's value. And also…
Avba
  • 14,822
  • 20
  • 92
  • 192
16
votes
2 answers

Passing an object created with SubFactory and LazyAttribute to a RelatedFactory in factory_boy

I am using factory.LazyAttribute within a SubFactory call to pass in an object, created in the factory_parent. This works fine. But if I pass the object created to a RelatedFactory, LazyAttribute can no longer see the factory_parent and fails. This…
Chris
  • 5,664
  • 6
  • 44
  • 55
11
votes
1 answer

How can I declare a private property within a named category?

I know about the possibility of declaring private properties on a class by putting them inside an unnamed category on that class declared in the implementation (.m) file of that class. That's not what I want to do. I'm dealing with a named category…
8
votes
2 answers

How to get the associated data of `OneToOneField()` in Django?

I'm working in Django 2.0 I have a model Note to save note and two another models to add color labels to the note. class Note(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE) title = models.CharField(max_length=250,…
Anuj TBE
  • 9,198
  • 27
  • 136
  • 285
6
votes
1 answer

Swift objc_getAssociatedObject always nil

I am trying to associated a property to an Array Extension: private var AssociatedObjectHandle: String = "BlaBLabla" extension Array { var emptyIndex:Int { mutating get { if let object = objc_getAssociatedObject(self,…
YanivH
  • 539
  • 4
  • 18
6
votes
2 answers

Cakephp 3.0 Save associated model

I am learning cakePHP 3.0 and have some problem with saving associated data on my model. I try to save a Client with associated data of ClientPreferences ClientTable class ClientsTable extends Table { public function initialize(array $config) …
3
votes
1 answer

objc_setAssociatedObject with nil to remove - is policy checked?

In the documentation... objc_setAssociatedObject Sets an associated value for a given object using a given key and association policy. void objc_setAssociatedObject(id object, void *key, id value, objc_AssociationPolicy policy) Parameters object…
unsynchronized
  • 4,828
  • 2
  • 31
  • 43
2
votes
1 answer

Swift strange EXC_BAD_ACCESS crash with objc_setAssociatedObject and objc_registerClassPair

I face a very strange crash on Swift. Xcode 11.3.1 Swift 5 Case 1 class TestObject { var deinitExecution: (() -> Void)? deinit { // comment this to avoid crash deinitExecution?() } } private var…
Yanni
  • 580
  • 2
  • 6
  • 21
2
votes
1 answer

Rails querying an associated models scope

I am struggling to find a suitable solution for what should be a simple task. Essentially I have a category model which has many posts. The post belongs to the category. I am displaying categories as part of a search form as well as a number of…
Torrm
  • 153
  • 1
  • 14
2
votes
1 answer

NSManagedObjectContext deallocation issues - (Swift | Associated Objects)

I am hoping someone can explain why associated objects in the following example do not get automatically deallocated when the source/host object is deallocated. This example code below is somewhat contrived (apologies in advance), but it explains…
So Over It
  • 3,668
  • 3
  • 35
  • 46
2
votes
2 answers

Rails: updating joined Active Model's attribute

I'm new to RoR. My question is about updating associated Active Model's attr. class User has_many :toys end class Toy belongs_to :user end and I have a page with a form of user where I can update user's attributes, and also certain attributes…
kong
  • 423
  • 1
  • 4
  • 12
2
votes
0 answers

Weak reference to 'associating' object in associated object

The following code results in an NSInternalConsistencyException: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'An instance 0x7fd3a1010000 of class UIScrollView was deallocated while key value observers were…
1
2 3 4