2015-11-30 31 views
0

我試圖訪問一個字符的字符串,但我發現了以下錯誤:斯威夫特:無法下標類型的值又名「陣列<Character>」

error: cannot subscript a value of type 'Array<_Element>' (aka 'Array<Character>') 
     finalString = characters [ 5 ] 

這裏是我的代碼:

//我的字符串:

var str = "Hello, playground" 

//我的字符串轉換成字符數組:

let characters = Array(str.characters) 

// string這裏我要轉移的角色:

var finalString:String 

finalString = characters [ 5 ] 

但在最後一行代碼是我得到的錯誤。你們中的任何一個人都知道我在做錯什麼或解決這個錯誤的方法。

我會很感激你的幫助

回答

1

你有一個字符數組,你需要把這一回一個字符串,以得到你想要的。試試這個:

// my string: 
var str = "Hello, playground" 

// I'm converting the string into array of characters: 
let characters = Array(str.characters) 

// String where I want to transfer the character: 
var finalString = String(characters[ 5 ]) 
相關問題