Swift 陣列內隨機排序

Henry Huang
1 min readJun 15, 2018

有時候可能會需要對陣列內的值進行隨機的排序,雖然也可以自己寫,但有時候還是需要更快更高效的方式來過過癮,就是import GameplayKit,使用裡面的GKRandomSource來達到效果:

import GameplayKitvar array = [1,2,3,4,5]
let arrayRandom = GKRandomSource.sharedRandom().arrayByShufflingObjects(in: array)
print(arrayRandom) //[4,2,5,3,1]

詳細可再參考蘋果文檔:https://developer.apple.com/documentation/gameplaykit/gkrandomsource#//apple_ref/occ/instm/GKRandomSource/arrayByShufflingObjectsInArray:

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Henry Huang
Henry Huang

Written by Henry Huang

大學讀企管/退伍後在業務圈打滾6年後突然有一天決定投入完全沒接觸過的程式設計/目前為一位iOS Developer/熱愛NBA/dafu1231@gmail.com

No responses yet

Write a response