0

I'm working on a project thatand am simply trying to create a object instance of a simple custom class:

import Foundation

class Set {

    private var gam1: Int!
    private var gam2: Int!

    init (gam1: Int, gam2: Int) {
        self.gam1 = gam1
        self.gam2 = gam2
    }

    //returns set info as array
    func getStats () -> [Int] {
        let arr = [gam1!, gam2!]
        return arr
    }

}

The class simply stores a few variables for use later and I want an array of such objects to store several values. However, when I try to create a n instance of the class in a different class I get errors:

import Foundation
    class AnotherClass {

var mySet = Set(gam1: 6, gam2: 5) //error 1

   //array of set objects
    var setArray = [Set]()  // error 2

    //returns array of set objects
    func getSets () -> [Set] {  //error 3
    return setArray
    }

}

The errors state:

  1. Cannot find an initializer for type 'Set' that accepts an argument list of type '(gam1: Int, gam2: Int)'

  2. Cannot invoke initializer for type '[Set]' with no arguments

and

  1. Reference to generic type 'Set' requires arguments in <...>

Any ideas of what the issue is here? could the 'Set' name of the class be conflicting with a reserved keyword?

Many thanks, Kw

1
  • Rename your class. It seems to interfere with the existing Set collection type. Commented May 8, 2016 at 16:23

2 Answers 2

3

The issue that you are having is due to the naming conflict between Set in the Swift standard library and the one you defined.

This is never a good idea. Instead, give it a more descriptive name (and one that doesn't conflict with anything else). For instance, call it gamHolder and initialize it gamHolder(gam1: <an int>, gam2: <another int>).

Also, if you have defined variables inside the init function they do not need to be forced unwrapped optionals.

For example:

class myClass {
    var myInt: Int

    init(anInt: Int) {
        myInt = anInt
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

This was definitely an issue however I am still getting a problem after creating a new class renamed TennisSet and pasting the code in with a class name also of TennisSet:
XCode is saying that the class is an unresolved identifier:
Could you put your code on pastebin? It would help to get some context.
0

You defined 2 parameters in your init method (since gam1 and gam2 are not optional). So, you have 2 solutions:

1 - You add parameters into your init call (like this):

 var mySet = Set(gam1: 1, gam2: 2) 

2 - You change gam1 and gam2 to optionals and you add a zero parameters init:

class Set {

    private var gam1: Int?
    private var gam2: Int?

    init() {
    }

    init(gam1: Int, gam2: Int) {
        self.gam1 = gam1
        self.gam2 = gam2
    }

    // returns set info as array
    func getStats() -> [Int] {
        let arr = [gam1!, gam2!]
        return arr
    }
}

So, you will be able to call it like this: var mySet = Set()

Also: be careful: Set is a class used in the Swift standard library. It's never a good idea to use same class names than Swift Standard Library. A name like TenisSet would be better.

Edit:

Here is a final working example with a renamed class:

class TenisSet {

    private var gam1: Int?
    private var gam2: Int?

    init() {
    }

    init(gam1: Int, gam2: Int) {
        self.gam1 = gam1
        self.gam2 = gam2
    }

    // returns set info as array
    func getStats() -> [Int] {
        let arr = [gam1!, gam2!]
        return arr
    }
}

class AnotherClass {

    var mySet = TenisSet()

    // array of set objects
    var setArray = [TenisSet]() 

    // returns array of set objects
    func getSets() -> [TenisSet] {
        return setArray
    }
}

2 Comments

Thanks the <Set> keyword was definitely part of the problem but I still cant reference a new class called TennisSet.
I edited my answer with a working example at the bottom

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.