![](/img/trans.png)
[英]cannot convert value of type 'String' to type 'NSString' in coercion when i use self swift2 with ubuntu 16.04
[英]cannot convert value of type 'NSString' to type 'String' in coercion swift2 with ubuntu 14.04
我對NSString的處理有問題,我不是該屬性的專家,並且我不知道為什么無法轉換為字符串,這是代碼的一部分:
var countL = LEXEMAS.count-1;
var countP = PATRONES.count-1;
var patternIndex:Int = -1;
for indexL in 0..<countL {
var input:NSString = NSString(string: LEXEMAS[indexL]);
var range:NSRange = NSMakeRange(0, input.length)
TOKENS.append("no existe");
for indexP in 0..<countP {
var regex = try? NSRegularExpression(pattern: PATRONES[indexP], options: NSRegularExpressionOptions(rawValue: 0))
var matches = regex!.matchesInString(LEXEMAS[indexL], options: NSMatchingOptions(rawValue: 0), range: range)
if matches.count > 0 {
TOKENS[indexL] = NOMBRES[indexP];
print(NOMBRES[indexP] + " " + (input as String) + "\n");
break;
}
}
}
這是錯誤的詳細信息:
main.swift:75:46: error: cannot convert value of type 'NSString' to type 'String' in coercion
print(NOMBRES[indexP] + " " + (input as String) + "\n");
^~~~~
我找到了答案,只有我必須在輸入var中指定值的類型,如下所示:
print(NOMBRES[indexP] + " " + String(input) + "\n")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.