Can anyone show me what wrong with my code?
code in playground
error detail
protocol PersonWithName: class {
var personName: String {get set}
init(name: String)
}
class NameCard<PersonType: SKSpriteNode where PersonType: PersonWithName> {
var person: PersonType
init() {
self.person = PersonType(name: "No Name") // this line error.
}
}
For shorter explanation I replaced SKSpriteNode with Any because you have to deal with the designated init() of SKSpriteNode later in more detail.. For more details on Initializer Requirements in protocols, take a look at this section of Apple's Developer Guide
protocol PersonWithName: class {
var personName: String {get set}
init(name: String)
}
class NameCard<PersonType: Any where PersonType: PersonWithName> {
var person: PersonType
init() {
self.person = PersonType(name: "No Name")
}
}
class ExampleClass: PersonWithName {
var personName: String = ""
required init(name: String) {
personName = name
}
}
var test = NameCard<ExampleClass>()
print(test.person.personName) // "No Name"
My current imperfect solution.#FlorianWeßling
protocol PersonWithName: class {
var personName: String {get set}
init(name: String)
}
protocol SKSpriteNodeProtocol {
var position: CGPoint {get set}
var size: CGSize {get set}
//or more properties you want
}
class NameCard<PersonType: SKSpriteNodeProtocol where PersonType: PersonWithName> {
var person: PersonType
init() {
self.person = PersonType(name: "No Name")
}
}
extension SKSpriteNode : SKSpriteNodeProtocol {}
class ExampleClass: SKSpriteNode, PersonWithName {
var personName: String = ""
required init(name: String) {
personName = name
super.init(texture: nil, color: UIColor.clearColor(), size: CGSizeZero)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
var test = NameCard<ExampleClass>()
print(test.person.personName) // "No Name"
If it is only important to access a SKSpriteNode within the NameCard then you might add a simple variable node and remove the inheritance from SKSpriteNode:
protocol PersonWithName {
var personName: String {get set}
init(name: String)
}
class NameCard<PersonType: PersonWithName> {
var person: PersonType
var node: SKSpriteNode?
init() {
self.person = PersonType(name: "No Name")
}
}
class ExampleClass: PersonWithName {
var personName: String = ""
required init(name: String) {
personName = name
}
}
var test = NameCard<ExampleClass>()
print(test.person.personName) // "No Name"
print(test.node?.description) // currently nil
Related
have been fighting the code for some time now, and can't resolve the issue of: Type 'American' does not conform to protocol 'Food'
protocol Food {
var type: String { get }
var ingredient1: String { get }
var price: Int { get set}
func showHistory()
mutating func transfer()
init(type: String)
init(ingredient1: String)
}
struct American: Food {
let type: String
let ingredient1: String
var price: Int = 125
init(type: String, ingredient1: String) {
self.type = type
self.ingredient1 = ingredient1
}
func showHistory() {
print("American history")
}
mutating func transfer() {
print("transfering burgers")
}
}
I doubt you intended to separate your inits into two separate calls which is causing your error. You could solve this by implementing two separate inits as well but then you'd have to initialize both properties in separate inits which will give you an error
protocol Food {
var type: String { get }
var ingredient1: String { get }
var price: Int { get set}
func showHistory()
mutating func transfer()
init(type: String, ingredient1: String)
}
struct American: Food {
let type: String
let ingredient1: String
var price: Int = 125
init(type: String, ingredient1: String) {
self.type = type
self.ingredient1 = ingredient1
}
func showHistory() {
print("American history")
}
mutating func transfer() {
print("transfering burgers")
}
}
Example:
protocol Animal {
#objc dynamic var name: String {get set}
#objc dynamic var age: Int {get set}
}
class Dog: Animal {
#objc dynamic var dogBreed: DogBreed? = nil
}
class Cat: Animal {
#objc dynamic var catBreed: CatBreed? = nil
}
class Bird: Animal {
#objc dynamic var birdBreed: BirdBreed? = nil
}
class Person: Object {
#objc dynamic var name = ""
#objc dynamic var picture: NSData? = nil
let pets = List<Animal>()
}
I'm new in Realm, but I know Realm does not support polymorphism. Is there any option to solve this example?
In swift is it possible to declare a method property not as a type but as a protocol?
like:
protocol myProtocol {
var data1: String {get set}
var node: Int {get set}
}
class myData: myProtocol {
var data1: String = "Boo"
var node: int = 10
}
class myClass {
func myFunc(data: myProtocol) {
data.data1 = "Hello"
}
}
Basically I want to say to the method look I don't care about the type. As long as the object conforms to the protocol its ok
Yes this is fine, but to modify data you will have to declare a class only protocol.
protocol MyProtocol: AnyObject {
var data1: String {get set}
var node: Int {get set}
}
class MyData: MyProtocol {
var data1: String = "Boo"
var node: Int = 10
}
class MyClass {
func myFunc(data: MyProtocol) {
data.data1 = "Hello"
}
}
I've also fixed the capitalisation of your classes.
You can use associatedtype.
protocol MyProtocol {
associatedtype CustomData
var data1: String { get set }
var node: Int { get set }
func myFunc(data: CustomData)
}
class MyData: MyProtocol {
func myFunc(data: String) {
print(data)
}
var data1: String = "Boo"
var node: Int = 10
}
Also, you should use PascalCase for both protocols and classes, and Int is the integer type for swift.
EDIT:
I misunderstood your question. You can also specify a function parameter by an abstract protocol, not just a class or a struct!
protocol MyProtocol {
var data1: String { get set }
var node: Int { get set }
}
class MyData: MyProtocol {
var data1: String = "Boo"
var node: Int = 10
}
class MyClass {
func myFunc(data: MyProtocol) {
print(data.data1)
}
}
let data = MyData()
let instance = MyClass()
instance.myFunc(data: data) // Boo
Im just learning Swift 4 and I have some troubles trying to access a property of an struct that have to implement 2 protocols
here is my struct
struct FigureA {
static func load() -> Model {
return Model.make(
name: "FigureName",
status: "Painted",
image: UIImage(named: "FigureA"),
description: "Good figure")
}
}
here the protocol 1
protocol ListModel: class {
var name: String { get }
var status: String { get }
var image: UIImage? { get }
}
here the protocol 2
protocol DetailModel: ListModel {
var categoryName: String { get }
var modelDescription: String? { get }
}
And I want to get the access to the description of the Struct but I don't know how at all.
Can someone give me a bit of light.
Here is good start for you:
protocol BaseProtocol {
var id: Int { get set }
}
protocol PersonProtocol: BaseProtocol {
var firstName: String { get set }
var lastName: String { get set }
var name: String { get }
}
struct Person: PersonProtocol {
var id: Int
var firstName: String
var lastName: String
var name: String { return firstName + " " + lastName }
}
//≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠≠
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//Create Struct Instance & Print properties.
let person = Person(id: 1001, firstName: "Manish", lastName: "Rathi")
print(person.id)
print(person.firstName)
print(person.lastName)
print(person.name)
}
}
#HappyCoding 😊
Swift 2
Xcoode 7.3
I try to store this:
var someArray = [Class1(id: 1, titel: "Titel 2", something: Class2(somevar: 20))]
with NSUSerDefaults:
let arrayData = NSKeyedArchiver.archivedDataWithRootObject(someArray)
NSUserDefaults.standardUserDefaults().setObject(arrayData, forKey: "array")
My classes look like this:
class Class1 {
var id: Int
var titel: String!
var something: Class2
init(id: Int, titel: String, something: Class2) {
self.id = id
self.titel = titel
self.something = something
}
required init(coder aDecoder: NSCoder) {
self.id = aDecoder.decodeIntForKey("id")
self.titel = aDecoder.decodeStringForKey("titel")
???
}
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(id, forKey: "id")
aCoder.encodeObject(titel, forKey: "titel")
???
}
class Class2: Class1
{
var somevar: Int
init(setback: Int) {
self.somevar = somevar
}
}
What do I need to add in those classes?
(Have to add some mor details; but I think it's self-explaining)
Uses #FLX code sample and ensure every object in your object's tree also conforms to NSCoding.
it's very easy.
write:
self.something = aDecoder.decodeObjectForKey("something") as! Class2
read:
aCoder.encodeObject(something, forKey: "something")