使用Scrollview时出错 - Swift

时间:2016-07-14 08:12:33

标签: swift loops uiscrollview

This are my codes, looking for a possible solution to the error

//
// ViewController.swift
// Login
//
// Created by MAC on 14/07/2016.
// Copyright 2016 VerseCom. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var scrollview: UIScrollView!

    let WIDTH : CGFloat = 320
    let HIEGHT : CGFloat = 568

    override func viewDidLoad() {
        super.viewDidLoad()

        var i in 1..4 {
            let img = UIImage(named: "/(i)")
            let imgview = UIImageView(image: img)
            self.scrollview.addSubview(imgview)

        }

        }
    }

1 个答案:

答案 0 :(得分:0)

我认为您希望使用for-In循环循环几次,但是在代码中以这种方式使用它:

var i in 1...4 {
 ...
}

这是你应该如何使用它:

使用for-in循环迭代序列,例如数字范围,数组中的项或字符串中的字符。

此示例打印五次表中的前几个条目:

for index in 1...5 {
    print("\(index) times 5 is \(index * 5)")
}

// 1 times 5 is 5
// 2 times 5 is 10
// 3 times 5 is 15
// 4 times 5 is 20
// 5 times 5 is 25

另一个例子:

let base = 3
let power = 10
var answer = 1
for _ in 1...power {
    answer *= base
}
print("\(base) to the power of \(power) is \(answer)")
// Prints "3 to the power of 10 is 59049"

另一个:

let names = ["Anna", "Alex", "Brian", "Jack"]
for name in names {
    print("Hello, \(name)!")
}
// Hello, Anna!
// Hello, Alex!
// Hello, Brian!
// Hello, Jack!

和另一个:

let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]
for (animalName, legCount) in numberOfLegs {
    print("\(animalName)s have \(legCount) legs")
}
// ants have 6 legs
// cats have 4 legs
// spiders have 8 legs

From Apple documentation