-3

How would I fix this problem?

Here's the code

//
//  TTTImageView.swift
//  TicTacToe
import UIKit

class TTTImageView: UIImageView {

    var player:String?
    var activated:Bool! = false

Problem is here and states "Method 'setPlayer' with objective c selector 'setPlayer:'conflicts with setter for 'player'with same objective c selector"

   func setPlayer (_player:String){
        self.player = _player

        if activated == false{
            if _player == "x"{
                self.image = UIImage(named: "x")
            }else{
                self.image = UIImage(named: "o")
            }
            activated = true
        }
    }
}

I haven't tried anything yet but will greatly appreciate any help.

Pang
  • 9,564
  • 146
  • 81
  • 122

3 Answers3

-1

There error is explained here: https://stackoverflow.com/a/28500768/3149796

There are lots of great Swift features that you can use instead of what you've got. Try a property observer (didSet) instead of creating a custom setter function. You can also clean up your code and make it safer with an optional binding (if let) and a where clause.

Also your activated property shouldn't be implicitly unwrapped (Bool!), but rather just Bool.

import UIKit

class TTTImageView: UIImageView {

    var activated: Bool = false

    var player: String? {
        didSet {
            if let player = self.player where self.activated == false {
                if self.player == "x" {
                    self.image = UIImage(named: "x")
                } else {
                    self.image = UIImage(named: "o")
                }
                self.activated = true
            }
        }
    }

}
Community
  • 1
  • 1
Patrick Lynch
  • 2,742
  • 1
  • 16
  • 18
  • That Would Not Work Because I Would Need It To Be A func To Carry Out A Command – Ethan Marcus Jul 01 '15 at 03:49
  • Property observers can execute any code that can be executed in a function, and will do so every time you set the player, such as `imageView.player = "x"`. If you mean you need to execute this image setting code from else where as well, you could create a method called, say, `updatePlayer()` that can be called from the property observer and anywhere else you like. – Patrick Lynch Jul 01 '15 at 05:50
-1

I had the same problem and I was able to fix it by using @nonobjc

More information here: Swift 2, method 'setOn' with Objective-C selector 'setOn:' conflicts with setter for 'on' with the same Objective-C selector

Community
  • 1
  • 1
alexiscrack3
  • 148
  • 1
  • 7
  • Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Baum mit Augen Oct 17 '16 at 20:53
-2

change setPlayer to Player

I hope this helps

Jeylani Osman
  • 199
  • 3
  • 13
  • Hi Anish, thanks for your answer, however answers on SO should explain _why_ or _how_ a solution answers the question. Please see [How do I write a good answer?](http://stackoverflow.com/help/how-to-answer) – Tim Malone May 26 '16 at 23:08