Ios 创建多个数组,这些数组将相加为一个最终数组。调试问题。Swift和Xcode
我正在尝试创建一个抽认卡应用程序。我也成功地获得了这个应用程序,我可以在其中浏览一系列照片(见下面的代码)Ios 创建多个数组,这些数组将相加为一个最终数组。调试问题。Swift和Xcode,ios,arrays,swift,xcode,debugging,Ios,Arrays,Swift,Xcode,Debugging,我正在尝试创建一个抽认卡应用程序。我也成功地获得了这个应用程序,我可以在其中浏览一系列照片(见下面的代码) 导入UIKit 类SecondViewController:UIViewController、UIGestureRecognitizerDelegate{ @IBAction func home(\发送方:任意){ 性能检查(标识符为“主页”,发送者为自我) } @IBOutlet弱var imgPhoto:UIImageView! var imageList:[字符串]=[“鳄鱼”、“苹
导入UIKit
类SecondViewController:UIViewController、UIGestureRecognitizerDelegate{
@IBAction func home(\发送方:任意){
性能检查(标识符为“主页”,发送者为自我)
}
@IBOutlet弱var imgPhoto:UIImageView!
var imageList:[字符串]=[“鳄鱼”、“苹果”、“平衡”、“球”、“芭蕾舞演员”、“气球”、“铃铛”、“腰带”、“黑色”、“毛毯”、“搅拌机”、“积木”、“金发”、“血”、“吹”、“蓝色”、“保龄球”、“泡泡”、“欺凌”、“日历”、“城堡”、“大提琴”、“蛤蜊”、“夹子”、“拍手”、“爪子”、“清洁”、“攀爬”、“夹子”、“云”、“冷”、“颜色”、“爬行”、“卷曲空气”“美元”,“海豚”,“大象”,“精灵”,“睫毛”,“秋天”,“鱼缸”,“旗帜”,“人字拖”,“浮子”,“地板”,“花”,“毛茸茸的”,“长笛”,“飞”,“汽油”,“女孩”,“冰川”,“高兴”,“眼镜”,“滑翔”,“闪光”,“地球”,“手套”,“胶水”,“守门员”,“高尔夫”,“草裙”,“水母”,“梯子”,“瓢虫”,“湖”,“羔羊”,“灯”,“云雀”,“笑”,“割草机”、“叶子”、“皮带”、“左”、“腿”、“柠檬”、“豹子”、“小妖精”、“字母”、“舔”、“救生员”、“举”、“灯泡”、“闪电”、“石灰”、“狮子”、“嘴唇”、“列表”、“听”、“骆驼”、“锁”、“原木”、“看”、“爱”、“午餐”、“融化”、“牛奶”、“橄榄”、“猫头鹰”、“桶”、“皮”、“枕头”、“飞行员”、“行星”、“木板”、“植物”、“盘子”、”“演奏”、“梅花”、“水管工”、“加号”、“北极熊”、“泳池”、“旱冰鞋”、“尺子”、“架子”、“傻瓜”、“雪橇”、“睡眠”、“袖子”、“切片”、“滑梯”、“滑梯”、“慢”、“微笑”、“电话”、“电视”、“郁金香”、“伞”、“情人节”、“小提琴”、“鲸鱼”、“轮子”、“木琴”、“黄色”]
设最大值=135
var imageIndex:NSInteger=0
重写func viewDidLoad(){
super.viewDidLoad()
//加载视图后执行任何其他设置。
imgPhoto.isUserInteractionEnabled=true
让leftSwipe=UISwipegestureRecognitor(目标:自我,操作:#选择器(滑动(手势:))
leftSwipe.cancelsTouchesInView=false
让rightSwipe=UISwipe手势识别器(目标:自身,操作:#选择器(滑动(手势:))
rightSwipe.cancelsTouchesInView=false
leftSwipe.direction=.left
rightSwipe.direction=.right
view.AddGestureRecognitor(左扫)
view.AddGestureRecognitor(右击)
}
func刷卡(手势:UIgestureRecognitor){
如果让swipeGesture=手势为?UISweepGestureRecognitor{
开关开关方向{
案例UISweepGestureRecognitzerDirection.right:
打印(“用户向右滑动”)
//先降低指数
imageIndex-=1
//检查索引是否在范围内
如果imageIndex<0{
imageIndex=最大值
}
imgPhoto.image=UIImage(名称:imageList[imageIndex])
案例UISweepGestureRecognitzerDirection.left:
打印(“用户向左滑动”)
//先增加指数
imageIndex+=1
//检查索引是否在范围内
如果imageIndex>最大值{
imageIndex=0
}
imgPhoto.image=UIImage(名称:imageList[imageIndex])
违约:
中断//停止代码/不编码任何内容。
}
}
但是我需要添加一个设置页面,以便用户可以选择他们想要显示的单词组,因此我尝试更改代码,以便将单词分成多个组,并将这些组设置为“活动”,以便用户可以在设置页面中操作它们(如果用户关闭某个词组,则该词组不再处于活动状态)。然而,我无法让这段新代码顺利运行,它充满了漏洞。你们中的任何人都能看到这段新代码有什么问题吗?到目前为止,我还没有创建设置页面,但当我运行新代码时,我应该能够像我的原始代码一样浏览所有图片,但目前这是不可能的。有任何帮助吗非常感谢。谢谢
import UIKit
class SecondViewController: UIViewController , UIGestureRecognizerDelegate {
@IBAction func home(_ sender: Any) {
performSegue(withIdentifier: "home", sender: self)
}
@IBOutlet weak var imgPhoto: UIImageView!
struct List {
let words: [String]
var active: Bool
}
let list1 = List(words:["lake", "lamb", "lamp", "lark", "leaf", "leash", "left", "leg", "lime", "lion", "lips", "list", "lock", "log", "look", "love", "lunch"], active: true)
let list2 = List(words: ["ladder", "ladybug", "laughing", "lawnmower", "lemon", "leopard", "leprechaun", "letters", "licking", "lifesaver", "lifting", "lightbulb", "lightning", "listen", "llama"], active: true)
let list3 = List(words: ["alligator", "balance", "ballerina", "balloon", "bowling", "cello", "colors", "curlyhair", "dollar", "dolphin", "elephant", "eyelashes", "gasoline", "goalie", "hula", "jellyfish", "olive", "pillow", "pilot", "polarbear", "rollerskate", "ruler", "silly", "telephone", "television", "tulip", "umbrella", "valentine", "violin", "xylophone", "yellow"], active: true)
let list4 = List(words: ["apple", "ball", "bell", "bubble", "castle", "fall", "fishbowl", "girl", "owl", "pail", "peel", "pool", "smile", "whale", "wheel"], active: true)
let list5 = List(words: ["planet", "plank", "plant", "plate", "play", "plum", "plumber", "plus"], active: true)
let list6 = List(words: ["black", "blanket", "blender", "blocks", "blond", "blood", "blow", "blue"], active: true)
let list7 = List(words: ["flag", "flipflop", "float", "floor", "flower", "fluffy", "flute", "fly"], active: true)
let list8 = List(words: ["glacier", "glad", "glasses", "glide", "glitter", "globe", "glove", "glue"], active: true)
let list9 = List(words: ["clam", "clamp", "clap", "claw", "clean", "climb", "clip", "cloud"], active: true)
let list10 = List(words:["sled", "sleep", "sleeves", "slice", "slide", "slime", "slip", "slow"], active: true)
let list11 = List(words: ["belt", "cold", "dolphin", "elf", "golf", "melt", "milk", "shelf"], active: true)
var imageIndex: NSInteger = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
imgPhoto.isUserInteractionEnabled = true
var wordLists = [list1, list2, list3, list4, list5, list6, list7, list8, list9, list10, list11]
var imageList: [String] {
let active = wordLists.reduce([]) { (result:[String], list:List) in
if list.active {
return result + list.words
} else {
return result
}
}
return active
}
let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(Swiped(gesture:)))
leftSwipe.cancelsTouchesInView = false
let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(Swiped(gesture:)))
rightSwipe.cancelsTouchesInView = false
leftSwipe.direction = .left
rightSwipe.direction = .right
view.addGestureRecognizer(leftSwipe)
view.addGestureRecognizer(rightSwipe)
}
func Swiped(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.right :
print("User swiped right")
// decrease index first
imageIndex -= 1
// check if index is in range
if imageIndex < 0 {
imageIndex = imageList
}
imgPhoto.image = UIImage(named: imageList[imageIndex])
case UISwipeGestureRecognizerDirection.left:
print("User swiped Left")
// increase index first
imageIndex += 1
// check if index is in range
if imageIndex > imageList {
imageIndex = 0
}
imgPhoto.image = UIImage(named: imageList[imageIndex])
default:
break //stops the code/codes nothing.
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
导入UIKit
类SecondViewController:UIViewController、UIGestureRecognitizerDelegate{
@IBAction func home(\发送方:任意){
性能检查(标识符为“主页”,发送者为自我)
}
@IBOutlet弱var imgPhoto:UIImageView!
结构列表{
让单词:[字符串]
激活变量:Bool
}
let list1=列表(单词:[“湖”、“羔羊”、“灯”、“云雀”、“树叶”、“皮带”、“左”、“腿”、“酸橙”、“狮子”、“嘴唇”、“列表”、“锁”、“日志”、“看”、“爱”、“午餐”),活动:真)
let list2=列表(单词:[“梯子”、“瓢虫”、“大笑”、“割草机”、“柠檬”、“豹子”、“小妖精”、“字母”、“舔”、“救生员”、“提升”、“灯泡”、“闪电”、“听”、“骆驼”),活动:真)
let list3=列表(单词:[“鳄鱼”、“平衡”、“芭蕾舞演员”、“气球”、“保龄球”、“大提琴”、“颜色”、“卷发”、“美元”、“海豚”、“大象”、“睫毛”、“汽油”、“守门员”、“草裙舞”、“水母”、“橄榄”、“枕头”、“飞行员”、“北极熊”、“旱冰鞋”、“尺子”、“傻瓜”、“电话”、“电视”、“郁金香”、“雨伞”、“情人节”、“小提琴“,”木琴“,”黄色“],活动:真)
let list4=列表(单词:[“苹果”、“球”、“钟”、“泡泡”、“城堡”、“秋天”、“鱼缸”、“女孩”、“猫头鹰”、“桶”、“皮”、“池”、“微笑”、“鲸鱼”、“轮子”),活动:真)
let list5=列表(单词:[“行星”、“木板”、“植物”、“盘子”、“游戏”、“梅花”、“水管工”、“加号”),活动:true)
let list6=列表(单词:[“黑色”、“毛毯”、“搅拌机”、“积木”、“金发”、“鲜血”、“吹”、“蓝色”],活动:真)
let list7=列表(单词:[“flag”、“flipflop”、“float”、“floor”、“floor”、“flower”、“fluffy”、“flut”、“fly”],活动:true)
let list8=列表(单词:[“冰川”、“高兴”、“眼镜”、“滑翔”、“闪光”、“地球”、“手套”、“胶水”),活动:真)
let list9=列表(单词:[“clam”、“clamp”、“clap”、“clamp”、“claw”、“clean”、“Gramp”、“clip”、“cloud”],活动:true)
let list10=列表(单词:[“雪橇”、“睡眠”、“袖子”、“切片”、“滑动”、“粘液”、“滑动”、“缓慢”],活动:真)
let list11=列表(单词:[“腰带”、“寒冷”、“海豚”、“精灵”、“高尔夫”、“融化”、“牛奶”、“sh
import UIKit
class SecondViewController: UIViewController , UIGestureRecognizerDelegate {
@IBAction func home(_ sender: Any) {
performSegue(withIdentifier: "home", sender: self)
}
@IBOutlet weak var imgPhoto: UIImageView!
struct List {
let words: [String]
var active: Bool
}
let list1 = List(words:["lake", "lamb", "lamp", "lark", "leaf", "leash", "left", "leg", "lime", "lion", "lips", "list", "lock", "log", "look", "love", "lunch"], active: true)
let list2 = List(words: ["ladder", "ladybug", "laughing", "lawnmower", "lemon", "leopard", "leprechaun", "letters", "licking", "lifesaver", "lifting", "lightbulb", "lightning", "listen", "llama"], active: true)
let list3 = List(words: ["alligator", "balance", "ballerina", "balloon", "bowling", "cello", "colors", "curlyhair", "dollar", "dolphin", "elephant", "eyelashes", "gasoline", "goalie", "hula", "jellyfish", "olive", "pillow", "pilot", "polarbear", "rollerskate", "ruler", "silly", "telephone", "television", "tulip", "umbrella", "valentine", "violin", "xylophone", "yellow"], active: true)
let list4 = List(words: ["apple", "ball", "bell", "bubble", "castle", "fall", "fishbowl", "girl", "owl", "pail", "peel", "pool", "smile", "whale", "wheel"], active: true)
let list5 = List(words: ["planet", "plank", "plant", "plate", "play", "plum", "plumber", "plus"], active: true)
let list6 = List(words: ["black", "blanket", "blender", "blocks", "blond", "blood", "blow", "blue"], active: true)
let list7 = List(words: ["flag", "flipflop", "float", "floor", "flower", "fluffy", "flute", "fly"], active: true)
let list8 = List(words: ["glacier", "glad", "glasses", "glide", "glitter", "globe", "glove", "glue"], active: true)
let list9 = List(words: ["clam", "clamp", "clap", "claw", "clean", "climb", "clip", "cloud"], active: true)
let list10 = List(words:["sled", "sleep", "sleeves", "slice", "slide", "slime", "slip", "slow"], active: true)
let list11 = List(words: ["belt", "cold", "dolphin", "elf", "golf", "melt", "milk", "shelf"], active: true)
var imageIndex: NSInteger = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
imgPhoto.isUserInteractionEnabled = true
var wordLists = [list1, list2, list3, list4, list5, list6, list7, list8, list9, list10, list11]
var imageList: [String] {
let active = wordLists.reduce([]) { (result:[String], list:List) in
if list.active {
return result + list.words
} else {
return result
}
}
return active
}
let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(Swiped(gesture:)))
leftSwipe.cancelsTouchesInView = false
let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(Swiped(gesture:)))
rightSwipe.cancelsTouchesInView = false
leftSwipe.direction = .left
rightSwipe.direction = .right
view.addGestureRecognizer(leftSwipe)
view.addGestureRecognizer(rightSwipe)
}
func Swiped(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.right :
print("User swiped right")
// decrease index first
imageIndex -= 1
// check if index is in range
if imageIndex < 0 {
imageIndex = imageList
}
imgPhoto.image = UIImage(named: imageList[imageIndex])
case UISwipeGestureRecognizerDirection.left:
print("User swiped Left")
// increase index first
imageIndex += 1
// check if index is in range
if imageIndex > imageList {
imageIndex = 0
}
imgPhoto.image = UIImage(named: imageList[imageIndex])
default:
break //stops the code/codes nothing.
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
class SecondViewController: UIViewController , UIGestureRecognizerDelegate {
@IBAction func home(_ sender: Any) {
performSegue(withIdentifier: "home", sender: self)
}
@IBOutlet weak var imgPhoto: UIImageView!
struct List {
let words: [String]
var active: Bool
}
let list1 = List(words:["lake", "lamb", "lamp", "lark", "leaf", "leash", "left", "leg", "lime", "lion", "lips", "list", "lock", "log", "look", "love", "lunch"], active: true)
let list2 = List(words: ["ladder", "ladybug", "laughing", "lawnmower", "lemon", "leopard", "leprechaun", "letters", "licking", "lifesaver", "lifting", "lightbulb", "lightning", "listen", "llama"], active: true)
let list3 = List(words: ["alligator", "balance", "ballerina", "balloon", "bowling", "cello", "colors", "curlyhair", "dollar", "dolphin", "elephant", "eyelashes", "gasoline", "goalie", "hula", "jellyfish", "olive", "pillow", "pilot", "polarbear", "rollerskate", "ruler", "silly", "telephone", "television", "tulip", "umbrella", "valentine", "violin", "xylophone", "yellow"], active: true)
let list4 = List(words: ["apple", "ball", "bell", "bubble", "castle", "fall", "fishbowl", "girl", "owl", "pail", "peel", "pool", "smile", "whale", "wheel"], active: true)
let list5 = List(words: ["planet", "plank", "plant", "plate", "play", "plum", "plumber", "plus"], active: true)
let list6 = List(words: ["black", "blanket", "blender", "blocks", "blond", "blood", "blow", "blue"], active: true)
let list7 = List(words: ["flag", "flipflop", "float", "floor", "flower", "fluffy", "flute", "fly"], active: true)
let list8 = List(words: ["glacier", "glad", "glasses", "glide", "glitter", "globe", "glove", "glue"], active: true)
let list9 = List(words: ["clam", "clamp", "clap", "claw", "clean", "climb", "clip", "cloud"], active: true)
let list10 = List(words:["sled", "sleep", "sleeves", "slice", "slide", "slime", "slip", "slow"], active: true)
let list11 = List(words: ["belt", "cold", "dolphin", "elf", "golf", "melt", "milk", "shelf"], active: true)
var imageIndex: Int = 0
var imageList: [String] {
let wordLists = [list1, list2, list3, list4, list5, list6, list7, list8, list9, list10, list11]
let active = wordLists.reduce([]) { (result:[String], list:List) in
if list.active {
return result + list.words
} else {
return result
}
}
return active
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
imgPhoto.isUserInteractionEnabled = true
let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(Swiped(gesture:)))
leftSwipe.cancelsTouchesInView = false
let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(Swiped(gesture:)))
rightSwipe.cancelsTouchesInView = false
leftSwipe.direction = .left
rightSwipe.direction = .right
view.addGestureRecognizer(leftSwipe)
view.addGestureRecognizer(rightSwipe)
}
func Swiped(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.right :
print("User swiped right")
// decrease index first
imageIndex -= 1
// check if index is in range
if imageIndex < 0 {
imageIndex = imageList.count - 1
}
imgPhoto.image = UIImage(named: imageList[imageIndex])
case UISwipeGestureRecognizerDirection.left:
print("User swiped Left")
// increase index first
imageIndex += 1
// check if index is in range
if imageIndex > imageList.count - 1 {
imageIndex = 0
}
imgPhoto.image = UIImage(named: imageList[imageIndex])
default:
break //stops the code/codes nothing.
}
}
}
}