Implementing the conversion and traversal of array array and Nsarray array in Swift

Source: Internet
Author: User

let stringArray: Array <String> = ["10", "20", "30", "40", "50"]
        for index in stringArray {
        
            println (index)
        }
        

The output is as follows:
.

There is no problem using Swift to traverse the Array array, you can use for ... in loop.


(2) Declare an NSArray array and traverse, the code is as follows:

     let stringNSArray: NSArray = ["10", "20", "30", "40", "50"]
        
        for index in stringNSArray {
        
            println (index)
        }

The output is as follows:
.

It can be seen that the NSArray array can also be directly declared and traversed in Swift.


(3) Declare an NSArray array, convert to Array array, and then traverse:

   let stringNSArray: NSArray = ["10", "20", "30", "40", "50"]
        let stringArray: [String] = stringNSArray as! [String]
        
        for index in stringArray {
        
            println (index)
        }

The output is as follows:
.


It can be seen that NSArray can be directly assigned to the Array array after type conversion, and then it can also be traversed.


(4) Declare an Array array, convert it to NSArray, and then traverse:

     let stringArray: Array <String> = ["10", "20", "30", "40", "50"]
        let stringNSArray: NSArray = stringArray
        for index in stringNSArray {
        
            println (index)
        }

The output is as follows:
.

It can be seen that the Array array can also be directly converted into NSArray and traversed.


      In summary, Swift is very compatible with NSArray in OC in terms of arrays. You can perform assignment conversion directly.















Copyright statement: This article is an original article by bloggers and may not be reproduced without the permission of the bloggers.

Swift Array array and NSArray array conversion and traversal

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.