[Swift] Any VS Anyobject

Source: Internet
Author: User

When you understand the meaning of @UIApplicationMain, you find something magical.

    Func application (application:uiapplication, didfinishlaunchingwithoptions launchoptions: [Nsobject:anyobject]?)- > Bool {                //Override point for customization after application launch.        return True    }

There was anyobject, what was that, [Nsobject:anyobject] and what, it felt weird.

Completely not adapt to it, and the previous beta version of the comparison will find the club???

    Func application (application:uiapplication, Didfinishlaunchingwithoptions launchoptions:nsdictionary?), Bool { c5/>//Override point to Customization after application launch.        return True    }

The original is used [Nsobject:anyobject] to replace the nsdictionary, that is why, what is the brackets do? ,“? "What is it again?

Instant doubt more, first of all to see the official explanation


Oh, it turns out that Anyobject is an instance of no type, so to be exact, any is an instance of anything without type, which can make basic data types, methods, closures, etc.

Do not think of the universal Id pointer, void these things, and the official also let in know the type of time, not the two of them, when necessary to recommend the use of ...

Oh, may understand any, anyobject these two concepts [nsobject:anyobject] What is the thing, and nsdictionary have what relationship?

The first reaction is that the former may be a form of the latter, access to information only to understand that the original is ... Xxx

There are so many statements in the dictionary:

A: var creditdictionary = dictionary<string,int> ()

B: var shorthanddictionary = [String:int] ()

C:var emptydictionary = shorthanddictionary = [:] (Empty dictionary can also be like this ~ OMG ~ ~)

So that we can simply understand [] is to take the corresponding class of instances, and then we will see similar arrays of declarations

A: var intarray = array<int> ()

B: var shorthandarray = [Int] ()

C: shorthandarray = [] (It's empty enough.) )

More about Nsarray and nsdictionary in Swift see the Meow:

http://onevcat.com/2014/06/walk-in-swift/

What about the problem, then? What are you doing, unpacking, and! ?

For details , see Code craftsman Explanation:

Http://www.cocoachina.com/swift/20140605/8687.html

Finally, put an official code on it.

Anyobject:

Let someobjects: [Anyobject] = [    Movie (Name: "2001:a Space Odyssey", Director: "Stanley Kubrick"),    Movie (name: "M Oon ", Director:" Duncan Jones "),    Movie (name:" Alien ", Director:" Ridley Scott ")]
<pre name= "code" class= "HTML" >for object in someobjects {let    movie = object as Movie    println ("Movie: ' \ (mov Ie.name) ', dir. \ (movie.director)]}//movie: ' 2001:a Space Odyssey ', dir. Stanley kubrick//Movie: ' Moon ', dir. Duncan J ones//Movie: ' Alien ', dir. Ridley Scott
For movie in Someobjects as [movie] {    println ("Movie: ' \ (movie.name) ', dir. \ (movie.director)")}//Movie: ' 2001:a Spa Ce Odyssey ', dir. Stanley kubrick//Movie: ' Moon ', dir. Duncan jones//Movie: ' Alien ', dir. Ridley Scott


Any:

var things = [any] () things.append (0) things.append (0.0) things.append (things.append) 3.14159 ("Hello ") Things.append ((3.0, 5.0)) Things.append (Movie (name:" Ghostbusters ", Director:" Ivan Reitman ")) Things.append ({(name : string), String in "Hello, \ (name)"})
for thing in things {switch thing {case 0 as int:println ("Zero as an Int") Case 0 as Double:println ("Zero as a Double") case-let Someint as Int:println ("an integer value of \" ( Someint) "Case let somedouble as double where somedouble > 0:println (" A positive Double value of \ (Somedoub Le) ") Case is double:println (" some other Double value that I don ' t want to print ") case let somestring as St  Ring:println ("A string value of \" \ (somestring) \ "") Case let (x, y) as (double, double): println ("An" (X, y) point at \ (x), \ (y) ") case let movie as Movie:println (" a movie called ' \ (movie.name) ', dir. \ (movie.directo R) Case-let Stringconverter as String-string:println (Stringconverter ("Michael") default:pri Ntln ("Something Else")}} 
<pre name= "code" class= "HTML" >for thing in things {switch thing {case 0 as in        T:println ("Zero as an int.") Case 0 as Double:println ("Zero as a Double") case-let Someint as Int:  println ("an integer value of \ (someint)") case let somedouble as Double where somedouble > 0:println ("A Positive double value of \ (somedouble) ") Case was double:println (" some other double value that I don ' t want to Print ") Case-let somestring as String:println (" a String value of \ "\ (somestring) \" ") Case let (x, y) as (Dou BLE, Double): println ("An (x, y) of point @ \ (x), \ (y)") case let movie as Movie:println ("a movie called ' \ (movie.name) ', dir. \ (movie.director) ") Case-let Stringconverter as String-String:println (Stringconverte R ("Michael")) default:println ("Something Else")}} 
Zero as a int//zero as a double//an integer value of 42//a positive Double value of 3.14159//A string value of "he" Llo "//an (x, Y) in 3.0, 5.0//a movie called ' Ghostbusters ', dir. Ivan reitman//Hello, Michael

Other related

Meow Stickers

http://swifter.tips/any-anyobject/

Official information

Https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TypeCasting.html






[Swift] Any VS Anyobject

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.