iOS Development--Error summary & common errors and warnings in development summary (eight)

Source: Internet
Author: User

Summary of common errors and warnings in development (eight)"[Anyobject]?does not has a member named generator" processing in Swift]

There is a small need to traverse all the viewcontroller of the current navigation controller stack. The Viewcontrollers property of the Uinavigationcontroller class itself returns a [anyobject]! array, but since my navigation controller itself may be nil, So I get to the Viewcontroller array as follows:

var myviewcontrollers: [Anyobject]? = Navigationcontroller?. Viewcontrollers

Gets the myviewcontrollers is a [anyobject]? Optional type, at this point if I go directly to traverse the myviewcontrollers, as shown in the following code

 for inch myviewcontrollers {    ...}

The compiler will indicate the following error:

" Generator "

In fact, either [Anyobject] or any other type, such as [String], will report the error. The reason is that the optional type is just a container, which is a different type than the value it wraps, i.e. [Anyobject] is an array type, but [Anyobject]? is not an array type. We can iterate over an array, but not iterate over a non-collection type.

There is such an interesting analogy on the stackoverflow that I put it directly on my laziness:

To understand the difference, let me do a real life example:you buy a new TV on ebay, the package is shipped to you, th E first thing you do are to check if the package (the optional) is empty (nil). Once you verify the TV was inside, you had to unwrap it, and put the box aside. You cannot use the TV while it's in the package. Similarly, an optional is a container:it are not the value it contains, and it doesn ' t has the same type. It can be empty, or it can contain a valid value.

All, the treatment here should be:

 
if let controllers = myviewcontrollers {      -in controllers {        ...    }}

iOS Development--Error summary & common errors and warnings in development summary (eight)

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.