Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Edit: this isn't working. I thought it was, but now I'm getting crashes I don't understand. If I figure it out, I'll come edit this more. Here's what I wrote before, which is wrong. Again, this is not working:</p> <blockquote> <p>I think I found the answer to this: Key-Value Coding.</p> <p>With Key-Value Coding, I can set up an instance variable of type id, and then dynamically identify the actual contents of the variable. I can put anything in it, even an int, and Key-Value methods will identify it for me. But what's even more awesome is that if I retreive the contents of a variable using Key-Value Coding, and the variable turns out to be an int, Key-Value Coding automatically turns it into an NSNumber for me.</p> <p>I will attempt to demonstrate.</p> <p>The main thing is to set up accessor statements for the variable I want to access. So, for this example, let's say the variable is called "identifyMe."</p> <pre><code>//in the interface: @property (copy) id accessIdentifyMe; //in the implementation: @synthesize accessIdentifyMe = identifyMe; </code></pre> <p>Now "identifyMe" is Key-Value Compliant. We're good to go.</p> <p>All I have to do is take the items in the variadic list, pass them into identifyMe one by one, and use Key-Value methods to identify the contents. Like so:</p> <pre><code>/*...assume all the following is happening inside a loop that </code></pre> <p>counts through the variadic list. I'll call the variadic list "variadicList." First, I take the current variadic item and put it into identifyMe, declaring it as type "id"*/ identifyMe = va_arg(variadicList, id);</p> <pre><code>/*Now I use a Key-Value method to check if identifyMe holds an </code></pre> <p>NSNumber. If it holds an int, it will automatically be returned as an NSNumber, you'll recall, so querying for an NSNumber is functionally the same as querying for an int.<br> Outside the loop I've set up a handy boolean called foundANumber in which to store the results of the query:*/ foundANumber = [[self valueForKey:@"identifyMe"] isKindOfClass:[NSNumber class]];</p> <p>That's given me the ability to separate the numbers from the objects. With that done, I can put the objects directly into my dictionary, and put the numbers into an NSNumber before I put them in the dictionary.</p> <p>And what this means at the other end, when calling this method from another object, is that I can call this method and put an int in the parameter list and not worry about causing a crash. Yay.</p> </blockquote> <p>Here's the reason this isn't working: KVC isn't magic. I thought it had some super special way of determining the type of a variable, but it doesn't. It just looks at the type declared in the accessor methods, and it can't independently verify any more than that. Darn. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload