为什么UITextView的文本没有被替换?

时间:2015-08-20 20:10:49

标签: ios swift uitextview

我有一小段代码,用于获取字符串,在库中查找单词,并从其他单词的随机数组中替换它们。出于某种原因,当我按下按钮时,没有任何反应!我在操场上工作了,所以我做错了什么?

应用代码:

import UIKit

extension Array {
    func randomItem() -> T {
        let index = Int(arc4random_uniform(UInt32(self.count)))
        return self[index]
    }
}

extension String {
    func replace(target: String, withString: String) -> String
    {
        return self.stringByReplacingOccurrencesOfString(target, withString: withString, options: NSStringCompareOptions.LiteralSearch, range: nil)
    }
}

class ViewController: UIViewController {


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        maintext.text = "The Dog Chases the Ball"
        self.submit.setTitle("Change It", forState: UIControlState.Normal)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBOutlet var maintext: UITextView!

    @IBOutlet var submit: UIButton!
    @IBAction func submitTapped(sender: UIButton) {
        self.submit.setTitle("Again", forState: UIControlState.Normal)

        var AllWords = maintext.text

        var WordsArray = AllWords.componentsSeparatedByString(" ")
        var Dog = ["Pup", "Canine", "Wolf"]
        var Ball = ["Frisbee", "Stick", "Car"]


        AllWords.replace("Dog", withString: Dog.randomItem()).replace("Ball", withString: Ball.randomItem())
    }

}

游戏场地:

import UIKit
import Foundation

extension Array {
    func randomItem() -> T {
        let index = Int(arc4random_uniform(UInt32(self.count)))
        return self[index]
    }
}

extension String
{
    func replace(target: String, withString: String) -> String
    {
        return self.stringByReplacingOccurrencesOfString(target, withString: withString, options: NSStringCompareOptions.LiteralSearch, range: nil)
    }
}

var Text = "The Dog Loves to Chase the Ball"


    //: Old Text Loaded as an array
    var AllWords = Text.componentsSeparatedByString(" ")

    //: New Word Library
    let Dog = ["Pup", "Canine", "Wolf"]
    let Ball = ["Frisbee", "Stick", "Car"]


for element in AllWords {
    Text.replace(element, withString: Dog.randomItem())
    Text.replace(element, withString: Ball.randomItem())

}

Text.replace("Dog", withString: Dog.randomItem()).replace("Ball", withString: Ball.randomItem())

谢谢!

2 个答案:

答案 0 :(得分:0)

您正在将maintext.text复制到AllWords,然后替换AllWords中的文字。 AllText现在有maintext.text中的内容副本,但未指向textview的text属性。替换AllWords中的文字后,您应该将AllWords分配给maintext.text

var AllWords = maintext.text

var WordsArray = AllWords.componentsSeparatedByString(" ")
var Dog = ["Pup", "Canine", "Wolf"]
var Ball = ["Frisbee", "Stick", "Car"]


AllWords.replace("Dog", withString: Dog.randomItem()).replace("Ball", withString: Ball.randomItem())

maintext.text = AllWords // You need set the textview's text here

如果仍然失败,请在设置文本时尝试跳回主线程:

dispatch_async(dispatch_get_main_queue(), { () -> Void in
    maintext.text = AllWords
})

答案 1 :(得分:0)

Swift中的String类型是一种值类型,表示任何更改都会创建String的新实例。

文本视图的字符串保持不变。

因此,在替换任务之后,您必须将值重新分配给文本视图

maintext.text = AllWords

PS:请用第一个小写字母命名变量