2016-02-17 1 views
1

Как преобразовать titleRange в Int !?Преобразование String.CharacterView.Index в int

var text: String = "Another neat aspect of playgrounds and the Assistant Editor is that you can inspect the output at different points in the program 
flow. So for instance, you could add another view of the UILable to the 
Assistant Editor before you’ve applied the changes to background color and 
corner radius to see what it looks like before that. Just click on the little white circle next to the “UILabel” to the right of the line where you’d like to inspect. " 


let titleRange = Int(text.characters.indexOf(".")) //Convert to Int! 

var attributedText: NSMutableAttributedString = NSMutableAttributedString(string: text as String) 
attributedText.addAttributes([NSFontAttributeName: UIFont.boldSystemFontOfSize(20)], range: NSRange(location: 0, length: titleRange)) 

ответ

-1

Я не уверен, но я думаю, что вы не можете конвертировать String.Index в Int, так что единственный способ преобразовать его в строку и после того, как к Int.

вы можете построить расширение, как:

extension String.Index { 
    func toInt() -> Int { 
     if let str:String = String(self) { 
      return Int(str)! 
     } 
     return 0 
    } 
} 

Вставь этот код, где вы храните свои расширения на .swift файл или всякий раз, когда вы хотите.

Смежные вопросы