The difference between Swift-anyobject and any

Source: Internet
Author: User

1, AnyObject: represents an object instance of any class type.

class Man {
}
 
class Woman {
}
 
let man = Man ()
let woman = Woman ()
var arr: [AnyObject] = [man, woman]
 
for people in arr {
    if let m = people as? Man {
        println ("This is a man")
    } else if let w = people as? Woman {
        println ("This is a woman")
    }
}

2, Any: a wider range, representing any type of instance except functions.


class Man {
}
 
class Woman {
}
 
var arr: [Any] = [Any] ()
arr.apppend (1)
arr.append ("hangge.com")
arr.append (Man ())
arr.append (Woman ())
 
for any in arr {
    switch any {
        case let any as Int:
            println ("This is an Int type")
        case let any as String:
            println ("This is a String type")
        case let any as Man:
            println ("This is a man type")
        case let any as Woman:
            println ("This is a woman type")
        default:
            println ("This is an unknown type")
    }
}

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.