"@" notation instead of initializer?

Discussion in 'iOS Programming' started by dantastic, Jun 10, 2013.

  1. dantastic macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #1
    I have just started coming across this use of an @ notation instead of a proper type initializer. Being quite simply an @ I've not been able to google any more info about it.

    What is is, I've found these

    @15 -> creates a NSNumber
    @[@"one", @"two", @three"] -> creates a NSArray

    Do anyone know where to find a bit more documentation around this? what the full capabilities are and any drawbacks?
     
  2. dantastic thread starter macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #3
    Thanks Matt!

    That's super cool stuff I must say. Now I know what to do today! :rolleyes:
     
  3. MattInOz macrumors 68030

    MattInOz

    Joined:
    Jan 19, 2006
    Location:
    Sydney
    #4
    The subscripting support for your own classes is pretty interesting.
     
  4. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #5
    Have you added subscripting support to your own classes? I haven't had occasion to do so.
     
  5. MattInOz macrumors 68030

    MattInOz

    Joined:
    Jan 19, 2006
    Location:
    Sydney
    #6
    Used it on a parent NSMangedObject to access the child objects in an ordered one to many relationship. Makes dealing with model objects in the controller easier and cleaner.
     
  6. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #7
    Cool. I'll have to bear that in mind.

    To add subscripting support to array-like objects, you have to add objectAtIndexedSubscript: and setObject:atIndexedSubscript: methods to your class, right? Anything else that you need to do?

    And what about support for dictionary subscripting? For those, I gather you implement objectForKeyedSubscript: and (for mutable dictionaries) setObject:forKeyedSubscript:
     

Share This Page