Soooo, I was reviewing this code from Chennai on Friday…
Soooo, I was reviewing this code from Chennai on Friday…
- Subject: Soooo, I was reviewing this code from Chennai on Friday…
- From: Alex Zavatone <email@hidden>
- Date: Mon, 02 May 2016 12:59:09 -0400
That alone should give you pause.
And I was trying to decipher how their code ended up transferring values from my object to a method that appears after a custom constructed alert.
This alert is simply a few views and a few buttons. No UIAlertView or UIAlertController is used.
What I found that shocked ad horrified me was that the object that was needed was being passed by adding it to the layer of the button by using an arbitrary key using KVC setValue: forKey: to an arbitrary key.
First, I was rather confused since I had created an accessor for this value off of a unique key, so this level of strangeness didn't need to be done, but secondly I had no idea that this was even possible, or even if it was a good idea.
The code looked something close to this:
UIButton *someButton = [UIButton buttonWithType:UIButtonTypeCustom];
someButton.tag = 1;
[[someButton layer] setValue: someImportantObject forKey:@"someArbitraryKey"];
And later on, when the button was tapped, they would get the object like so:
switch ([sender tag]) { // That button
case 1:
{
NSMutableDictionary *someImportantObject = [[sender layer]valueForKey:@"someArbitraryKey"];
[self doStuffWith: someImportantObject];
break;
}
I had no idea that using KVC that something as arbitrary as this was even possible - let alone if it is a good idea.
For it being a good idea, it's obscure as can be, so no. But does KVC allow you to be as arbitrary as that with *any* object (in this case, an NSDictionary) to another that originates from an NSObject with NSCoding protocols?
I was looking around to see how the NSKeyValueCoding protocols are adopted into NSObject and therein into a UILayer and I couldn't find where this happens. Where does this happen?
Also, though this can be done, it seems really really really really sketchy. Is it a valid approach - to use KVC to add arbitrary properties to any old NSObject or are there unexpected implications to be expected?
Thanks in advance. My mind is sufficiently warped enough from 1. thinking WHY someone would do this, 2. thinking it would be a good idea and 3. adding it as relied upon functionality in a shipping project.
The never ending list of surprises in this code give me PTSD.
Cheers and thanks,
Alex Zavatone
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden