[swift]为什么是 UITextView 的文本不被替换?

标签: UITextView ios Swift
发布时间: 2016/1/25 19:01:05
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有一个小小的代码意味着把字符串在库中,找到的话,取而代之的是从其他单词随机阵列。出于某些原因,当我按下按钮,没有任何反应!我做了它工作在操场上,所以我在做什么错了吗?

应用程序代码:

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())

谢谢!

解决方法 1:

您正在复制 maintext.textAllWords 然后替换文本在 AllWordsAllText现在有一份什么是 maintext.text ,但是不指向 textview 的 text 属性。 替换文本在后 AllWords ,您应该指定 AllWordsmaintext.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
})
官方微信
官方QQ群
31647020