The use of Swift-enumerateobjectsusingblock

Source: Internet
Author: User

enumerateobjectsusingblock:Methods that are not arrays are used in Nsarray. If you want to use it, you need an instance of Nsarray instead of an array.

import Foundationvar array: NSArray = ["Some", "strings", "in", "an", "array"]array.enumerateObjectsUsingBlock({ object, index, stop in    //your code})

If you have an existing array, use as to make an array into a nsarray

var cocoaArray = swiftArray as NSArray

Or you just import the foundation compiler will automatically bridge the array into Nsarray and then the Nsarray method will become available.

Swift1

You can only use the enumerate features of Swift:

for (index, value) in enumerate(array) {    // your code}
Swift2

Swift2,enumerate is no longer a free feature, now it's expanding in a protocol!

for (index, value) in array.enumerate() {    // your code}
Swift 3

Renamed to enumerated in Swift3,enumerate

for (index, value) in array.enumerated() {    // your code}

The use of Swift-enumerateobjectsusingblock

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.