Swift: 'Class.type' does not have member named 'variable' -
i have started swift programming language course , need on first program. using playground write anagram program in xcode v6.3.2. logic, first sorting words alphabetically , comparing them. error occurs when declaring word1 , word2 arrays , dictates 'anagram.type' not have member named 'word1'. believe logic correct. struggling understand why error occurs, , how solve it. have looked around online, having difficulty applying find own code. first time using stack overflow. constructive feedback welcomed.
import uikit class anagram{ let word1 :string let word2 :string init(word1: string, word2: string){ self.word1 = word1 self.word2 = word2 } var characters1 = array(word1) var characters2 = array(word2) characters1 = characters1.sort() characters2 = characters2.sort() var pos = 0 var match = true while pos < characters2.length && match { if characters1[pos] == characters2[pos]: pos = pos + 1 else: match = false } return match } let theanagram = anagram(word1: "abcd", word2: "dcba")
first- smozgur said in comments, need use function check whether or not anagram. secondly, can tell, array(string)
no longer works of swift 2.0. fix this, referred convert string array of characters swift 2.0. so, in summary, placed logic function , fixed creation of characters(1,2)
array. how got work:
class anagram{ let word1 : string let word2 : string init(word1: string, word2: string){ self.word1 = word1 self.word2 = word2 } func checkanagram () -> bool { var characters1 = array(word1.characters).sort() var characters2 = array(word2.characters).sort() var pos = 0 var match:bool = true while pos < characters2.count && match { if characters1[pos] == characters2[pos] { pos++ } else { match = false } } return match } } let trueanagram = anagram(word1: "abcd", word2: "dcba") trueanagram.checkanagram() //returns true let falseanagram = anagram(word1: "false", word2: "falze") falseanagram.checkanagram() //returns false.
sidenote: instead of using pos = pos + 1
, use pos++
.
if works you, please check answer. if not, comment , try again you.
Comments
Post a Comment