小编jjx*_*jjx的帖子

在swift中为所有出现的字符串着色

这段代码

var textSearch="hi"
var textToShow="hi hihi hi" 
var rangeToColor = (textToShow as NSString).rangeOfString(textSearch)
var attributedString = NSMutableAttributedString(string:textToShow)
attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.yellowColor() , range: rangeToColor)
TextView.attributedText=attributedString
Run Code Online (Sandbox Code Playgroud)

给我NSRange为TextView中的字符串着色.问题是我只返回第一次出现.如果单词包含"hi hihi hi",则仅第一个"hi"被着色.如何获取所有出现的字符串?

nsstring ios nsmutableattributedstring swift

13
推荐指数
2
解决办法
3730
查看次数

在Swift中进行异步GET/POST请求

我已经为make GET请求写了这个类

    class GETReq{
func HTTPsendRequest(request: NSMutableURLRequest, callback: (String, String?) -> Void) {
            let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {  (data, response, error) -> Void in

                dispatch_async(dispatch_get_main_queue()) {
                    if (error != nil) {
                        callback("", error.localizedDescription)
                    } else {
                        callback(NSString(data: data,encoding: NSUTF8StringEncoding)!, nil)
                    }
                }
            }
            task.resume()
    }   

func HTTPGet(url: String, callback: (String, String?) -> Void) {
    var request = NSMutableURLRequest(URL: NSURL(string: url)!)
    HTTPsendRequest(request, callback) }
}
Run Code Online (Sandbox Code Playgroud)

在另一个名为"ManageData"的类中

class ManageData{

    func makeTheRequest()->String{
        var makeReq:GETReq=GETReq();
        var ret:String="";

        makeReq.HTTPGet("http://www.example.com/fileExample.php") { (data:String, error:String?) -> Void in …
Run Code Online (Sandbox Code Playgroud)

http-get ios swift

1
推荐指数
1
解决办法
1960
查看次数