我是Xcode和刷卡的新手,非常感谢任何帮助。我已经为屏幕上半部分的单词列表创建了一个随机生成器(顶部标签,这包含在代码中)。我如何调整此代码,以便我可以在屏幕的下半部分使用不同的单词列表(底部标签不会附加到任何代码)?但是对于相同的点击按钮来随机化两个单词列表? The layout of the screen
导入UIKit
class ViewController:UIViewController { //放置标签和按钮
Uri gmmIntentUri = Uri.parse("geo:37.7749,-122.4194?z=10&q=restaurants");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
}
答案 0 :(得分:1)
使用扩展程序是全局的。你可以使用shuffle数组并打印报价。您可以声明引号数组全局,以便您可以在任何视图控制器中访问它。
var quotes = ["Geometrics", "Vectors", "Celebration", "Triangle", "Landscapes", "Seasons", "Snow", "Rain", "Sunrays", "Stencils", "Paint", "Graphics", "Graffiti", "Sports","Fashion",
"Ancient Greek", "Philosophers", "Fairy Tales", "Fantasy", "Clouds", "Mystery", "Time Clocks", "Canvas", "Tie-dye", "Glitter", "Dessert", "Desert", "Energy", "Astrology", "Solar Systems", "Sea", "Beach", "Sphere", "Roots", "Lights", "Darks", "Fire", "Air",
"Aperture", "Long exposure", "Portraits", "World", "Travel", "Architecture", "Freedom", "Old", "New", "Urban", "Lenses", "Fisheye", "Chords", "Music Notes", "Spices", "Herbs", "Natural", "Marbles", "Wood", "Trees", "Forests", "Interior",
"Mammals", "Reptiles", "Ocean", "Birds", "Photography", "Exposure", "Opaque", "Translucent", "Freestyle", "Spots", "Stripes", "Zig Zag", "Spiral", "Glass", "Feathers", "Calm", "Bulb", "Heat", "Cold", "Stitches", "Views", "Birds", "Sunset", "Earth"]
extension CollectionType {
/// Return a copy of `self` with its elements shuffled
func shuffle() -> [Generator.Element] {
var list = Array(self)
list.shuffleInPlace()
return list
}
}
extension MutableCollectionType where Index == Int {
/// Shuffle the elements of `self` in-place.
mutating func shuffleInPlace() {
// empty and single-element collections don't shuffle
if count < 2 { return }
for i in 0..<count - 1 {
let j = Int(arc4random_uniform(UInt32(count - i))) + i
guard i != j else { continue }
swap(&self[i], &self[j])
}
}
}
quotes = quotes.shuffle()
print(quotes[0]) // Print random quote
Shuffle扩展不是我写的。 积分 Nate Cook : https://stackoverflow.com/a/24029847/2803960