1

I have recently started a Swift programming language course and need some help on my first program. I am using playground to write an anagram program in Xcode v6.3.2. For my logic, I am first sorting the words alphabetically and then comparing them. The error occurs when declaring word1 and word2 as arrays and dictates 'Anagram.type' does not have a member named 'word1'. I believe my logic is correct. I am struggling to understand why this error occurs, and how to solve it. I have looked around online, but am having difficulty applying to what I find to my own code. This is my first time using Stack overflow. All constructive feedback is 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")
  • Are you in swift 2? In any case I believe you need a function to get what you need from your class. – smozgur Feb 07 '16 at 20:57

2 Answers2

1

First- as smozgur said in the comments, you need to use a function to check whether or not it is an anagram. Secondly, from what I can tell, Array(String) no longer works as of Swift 2.0. To fix this, I referred to Convert a String to an array of characters swift 2.0. So, in summary, I placed your logic into a function and fixed the creation of your characters(1,2) array. This is how I got this to 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, just use pos++.

If this works for you, please check my answer. If not, comment and I will try again to help you.

Community
  • 1
  • 1
jbcd13
  • 145
  • 8
  • 1
    Side-side note: instead of `pos++`, use `pos += 1`. [Increment operators are going away.](https://github.com/apple/swift-evolution/blob/master/proposals/0004-remove-pre-post-inc-decrement.md) – rickster Feb 08 '16 at 00:20
  • Thanks. I didn't know that – jbcd13 Feb 08 '16 at 01:52
0

hmmm..

adjust your code like below. below code no error...

let  word1 :String
let  word2 :String

class Anagram{


        var characters1 = Array(word1)
        var characters2 = Array(word2)
.
.
.
.

I guess that in your code word1,word2 is not yet exist because same class property is no order.. unlikely property of method

WoderMan
  • 53
  • 1
  • 12
  • This doesn't work. First, `arrayLiteral` gives an array of one value, the entire string. The user wants an array of each character of the string. Also, it still needs a function to work. – jbcd13 Feb 07 '16 at 23:22
  • edit my answer... but my opinion is same... The limits of my thoughts – WoderMan Feb 07 '16 at 23:28
  • I wrote an answer. Why on earth would I fix yours to say the same thing as mine? – jbcd13 Feb 07 '16 at 23:31