How can I access properties from my subclasses?












0















I having problem to access the properties from my subclasses. I have defined the superclass and than I've created the subclasses that inherited from super class. I Initialized my superclass inside my subclasses.



I have a class called Box. Inside this class I have a property called Offer that's of type OfferType.



var Offer: OfferType?



Than I've created the class OfferType and set some properties to inside it.



class OfferType {

var Name: String?
var Image: UIImage?
var Description: String?

init(Name: String, Image: UIImage, Description: String) {
self.Name = Name
self.Image = Image
self.Description = Description
}
}


Than I created 3 subclass of OfferType



class Discount: OfferType {

var Quantity: Int?

init() {
super.init(Name: "Discount", Image: UIImage(named: "Discount")!, Description: "nice")
}
}

class FreeShipping: OfferType {

var From: String?
var To: String?

init() {
super.init(Name: "FreeShiping", Image: UIImage(named: "FreeShipping")!, Description: "lalaa")
}
}

class BoGoF: OfferType {

lazy var SecondProduct = String()

init() {
super.init(Name: "BoGoF", Image: UIImage(named: "BoGoF")!, Description: "boh")
}
}


Now I'm trying to get the property of the subclass after Assign the subclass as property Offer inside BOX class.



I can't do it, the only options Xcode gives to me is to access the properties of superclass (OfferType )



I need to access the property Quantity of subclass Discount.



Here is Where I'm trying to access the properties



First I assign the subclass to the property in class Box



newBox.Offer = Discount



Now I'm trying to get the properties of Discount and It doesn't give to me



newBox.Offer. Just Here



I tried to cast it but It didn't work



enter image description here



My class newbox is defined like that



enter image description here



That's how the class Box is defined



Class Box










share|improve this question

























  • Edit this and show us what you what you are saying. I see the classes here, and that's a great start, but I don't see where you are trying to access any properties. I think I know what you are saying, and it's a quick fix, however, could you show us?

    – impression7vx
    Jan 3 at 0:59











  • Edited just right now

    – Felipe Fernandes
    Jan 3 at 1:11











  • Show us how newBox.offer is defined. Never mind, see my answer.

    – FJ de Brienne
    Jan 3 at 1:13


















0















I having problem to access the properties from my subclasses. I have defined the superclass and than I've created the subclasses that inherited from super class. I Initialized my superclass inside my subclasses.



I have a class called Box. Inside this class I have a property called Offer that's of type OfferType.



var Offer: OfferType?



Than I've created the class OfferType and set some properties to inside it.



class OfferType {

var Name: String?
var Image: UIImage?
var Description: String?

init(Name: String, Image: UIImage, Description: String) {
self.Name = Name
self.Image = Image
self.Description = Description
}
}


Than I created 3 subclass of OfferType



class Discount: OfferType {

var Quantity: Int?

init() {
super.init(Name: "Discount", Image: UIImage(named: "Discount")!, Description: "nice")
}
}

class FreeShipping: OfferType {

var From: String?
var To: String?

init() {
super.init(Name: "FreeShiping", Image: UIImage(named: "FreeShipping")!, Description: "lalaa")
}
}

class BoGoF: OfferType {

lazy var SecondProduct = String()

init() {
super.init(Name: "BoGoF", Image: UIImage(named: "BoGoF")!, Description: "boh")
}
}


Now I'm trying to get the property of the subclass after Assign the subclass as property Offer inside BOX class.



I can't do it, the only options Xcode gives to me is to access the properties of superclass (OfferType )



I need to access the property Quantity of subclass Discount.



Here is Where I'm trying to access the properties



First I assign the subclass to the property in class Box



newBox.Offer = Discount



Now I'm trying to get the properties of Discount and It doesn't give to me



newBox.Offer. Just Here



I tried to cast it but It didn't work



enter image description here



My class newbox is defined like that



enter image description here



That's how the class Box is defined



Class Box










share|improve this question

























  • Edit this and show us what you what you are saying. I see the classes here, and that's a great start, but I don't see where you are trying to access any properties. I think I know what you are saying, and it's a quick fix, however, could you show us?

    – impression7vx
    Jan 3 at 0:59











  • Edited just right now

    – Felipe Fernandes
    Jan 3 at 1:11











  • Show us how newBox.offer is defined. Never mind, see my answer.

    – FJ de Brienne
    Jan 3 at 1:13
















0












0








0








I having problem to access the properties from my subclasses. I have defined the superclass and than I've created the subclasses that inherited from super class. I Initialized my superclass inside my subclasses.



I have a class called Box. Inside this class I have a property called Offer that's of type OfferType.



var Offer: OfferType?



Than I've created the class OfferType and set some properties to inside it.



class OfferType {

var Name: String?
var Image: UIImage?
var Description: String?

init(Name: String, Image: UIImage, Description: String) {
self.Name = Name
self.Image = Image
self.Description = Description
}
}


Than I created 3 subclass of OfferType



class Discount: OfferType {

var Quantity: Int?

init() {
super.init(Name: "Discount", Image: UIImage(named: "Discount")!, Description: "nice")
}
}

class FreeShipping: OfferType {

var From: String?
var To: String?

init() {
super.init(Name: "FreeShiping", Image: UIImage(named: "FreeShipping")!, Description: "lalaa")
}
}

class BoGoF: OfferType {

lazy var SecondProduct = String()

init() {
super.init(Name: "BoGoF", Image: UIImage(named: "BoGoF")!, Description: "boh")
}
}


Now I'm trying to get the property of the subclass after Assign the subclass as property Offer inside BOX class.



I can't do it, the only options Xcode gives to me is to access the properties of superclass (OfferType )



I need to access the property Quantity of subclass Discount.



Here is Where I'm trying to access the properties



First I assign the subclass to the property in class Box



newBox.Offer = Discount



Now I'm trying to get the properties of Discount and It doesn't give to me



newBox.Offer. Just Here



I tried to cast it but It didn't work



enter image description here



My class newbox is defined like that



enter image description here



That's how the class Box is defined



Class Box










share|improve this question
















I having problem to access the properties from my subclasses. I have defined the superclass and than I've created the subclasses that inherited from super class. I Initialized my superclass inside my subclasses.



I have a class called Box. Inside this class I have a property called Offer that's of type OfferType.



var Offer: OfferType?



Than I've created the class OfferType and set some properties to inside it.



class OfferType {

var Name: String?
var Image: UIImage?
var Description: String?

init(Name: String, Image: UIImage, Description: String) {
self.Name = Name
self.Image = Image
self.Description = Description
}
}


Than I created 3 subclass of OfferType



class Discount: OfferType {

var Quantity: Int?

init() {
super.init(Name: "Discount", Image: UIImage(named: "Discount")!, Description: "nice")
}
}

class FreeShipping: OfferType {

var From: String?
var To: String?

init() {
super.init(Name: "FreeShiping", Image: UIImage(named: "FreeShipping")!, Description: "lalaa")
}
}

class BoGoF: OfferType {

lazy var SecondProduct = String()

init() {
super.init(Name: "BoGoF", Image: UIImage(named: "BoGoF")!, Description: "boh")
}
}


Now I'm trying to get the property of the subclass after Assign the subclass as property Offer inside BOX class.



I can't do it, the only options Xcode gives to me is to access the properties of superclass (OfferType )



I need to access the property Quantity of subclass Discount.



Here is Where I'm trying to access the properties



First I assign the subclass to the property in class Box



newBox.Offer = Discount



Now I'm trying to get the properties of Discount and It doesn't give to me



newBox.Offer. Just Here



I tried to cast it but It didn't work



enter image description here



My class newbox is defined like that



enter image description here



That's how the class Box is defined



Class Box







swift class inheritance subclass






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 5:54









Miraj50

2,78011025




2,78011025










asked Jan 3 at 0:53









Felipe FernandesFelipe Fernandes

33




33













  • Edit this and show us what you what you are saying. I see the classes here, and that's a great start, but I don't see where you are trying to access any properties. I think I know what you are saying, and it's a quick fix, however, could you show us?

    – impression7vx
    Jan 3 at 0:59











  • Edited just right now

    – Felipe Fernandes
    Jan 3 at 1:11











  • Show us how newBox.offer is defined. Never mind, see my answer.

    – FJ de Brienne
    Jan 3 at 1:13





















  • Edit this and show us what you what you are saying. I see the classes here, and that's a great start, but I don't see where you are trying to access any properties. I think I know what you are saying, and it's a quick fix, however, could you show us?

    – impression7vx
    Jan 3 at 0:59











  • Edited just right now

    – Felipe Fernandes
    Jan 3 at 1:11











  • Show us how newBox.offer is defined. Never mind, see my answer.

    – FJ de Brienne
    Jan 3 at 1:13



















Edit this and show us what you what you are saying. I see the classes here, and that's a great start, but I don't see where you are trying to access any properties. I think I know what you are saying, and it's a quick fix, however, could you show us?

– impression7vx
Jan 3 at 0:59





Edit this and show us what you what you are saying. I see the classes here, and that's a great start, but I don't see where you are trying to access any properties. I think I know what you are saying, and it's a quick fix, however, could you show us?

– impression7vx
Jan 3 at 0:59













Edited just right now

– Felipe Fernandes
Jan 3 at 1:11





Edited just right now

– Felipe Fernandes
Jan 3 at 1:11













Show us how newBox.offer is defined. Never mind, see my answer.

– FJ de Brienne
Jan 3 at 1:13







Show us how newBox.offer is defined. Never mind, see my answer.

– FJ de Brienne
Jan 3 at 1:13














1 Answer
1






active

oldest

votes


















0














Your newBox instance probably has the offer property defined as OfferType.



Even though you put an instance of Discount inside the offer property, the compiler has no way to know that since all you've told the compiler is that offer will contain an OfferType. It has no way to know the specific OfferType that will be contained there.



You have to cast offer to the type you expect. I suggest you look into as and is. Here is an example:



if let discount = newBox.offer as? Discount {
print("Discount Quantity is (discount.quantity)")
}


Here the as? keyword basically tells the compiler the following:




is newBox.offer a kind of Discount? If so return that, otherwise,
return nil.







share|improve this answer


























  • I tried to do that before, it's giving me error type dude.

    – Felipe Fernandes
    Jan 3 at 1:40











  • Please show how offer is defined in newBox. Also detail the error you are obtaining.

    – FJ de Brienne
    Jan 3 at 1:42











  • I edit it again

    – Felipe Fernandes
    Jan 3 at 1:42











  • last pictures I'm showing how it's defined

    – Felipe Fernandes
    Jan 3 at 1:44











  • You have to use as? not as!, otherwise an if...let isn't valid.

    – FJ de Brienne
    Jan 3 at 1:45












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54015051%2fhow-can-i-access-properties-from-my-subclasses%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Your newBox instance probably has the offer property defined as OfferType.



Even though you put an instance of Discount inside the offer property, the compiler has no way to know that since all you've told the compiler is that offer will contain an OfferType. It has no way to know the specific OfferType that will be contained there.



You have to cast offer to the type you expect. I suggest you look into as and is. Here is an example:



if let discount = newBox.offer as? Discount {
print("Discount Quantity is (discount.quantity)")
}


Here the as? keyword basically tells the compiler the following:




is newBox.offer a kind of Discount? If so return that, otherwise,
return nil.







share|improve this answer


























  • I tried to do that before, it's giving me error type dude.

    – Felipe Fernandes
    Jan 3 at 1:40











  • Please show how offer is defined in newBox. Also detail the error you are obtaining.

    – FJ de Brienne
    Jan 3 at 1:42











  • I edit it again

    – Felipe Fernandes
    Jan 3 at 1:42











  • last pictures I'm showing how it's defined

    – Felipe Fernandes
    Jan 3 at 1:44











  • You have to use as? not as!, otherwise an if...let isn't valid.

    – FJ de Brienne
    Jan 3 at 1:45
















0














Your newBox instance probably has the offer property defined as OfferType.



Even though you put an instance of Discount inside the offer property, the compiler has no way to know that since all you've told the compiler is that offer will contain an OfferType. It has no way to know the specific OfferType that will be contained there.



You have to cast offer to the type you expect. I suggest you look into as and is. Here is an example:



if let discount = newBox.offer as? Discount {
print("Discount Quantity is (discount.quantity)")
}


Here the as? keyword basically tells the compiler the following:




is newBox.offer a kind of Discount? If so return that, otherwise,
return nil.







share|improve this answer


























  • I tried to do that before, it's giving me error type dude.

    – Felipe Fernandes
    Jan 3 at 1:40











  • Please show how offer is defined in newBox. Also detail the error you are obtaining.

    – FJ de Brienne
    Jan 3 at 1:42











  • I edit it again

    – Felipe Fernandes
    Jan 3 at 1:42











  • last pictures I'm showing how it's defined

    – Felipe Fernandes
    Jan 3 at 1:44











  • You have to use as? not as!, otherwise an if...let isn't valid.

    – FJ de Brienne
    Jan 3 at 1:45














0












0








0







Your newBox instance probably has the offer property defined as OfferType.



Even though you put an instance of Discount inside the offer property, the compiler has no way to know that since all you've told the compiler is that offer will contain an OfferType. It has no way to know the specific OfferType that will be contained there.



You have to cast offer to the type you expect. I suggest you look into as and is. Here is an example:



if let discount = newBox.offer as? Discount {
print("Discount Quantity is (discount.quantity)")
}


Here the as? keyword basically tells the compiler the following:




is newBox.offer a kind of Discount? If so return that, otherwise,
return nil.







share|improve this answer















Your newBox instance probably has the offer property defined as OfferType.



Even though you put an instance of Discount inside the offer property, the compiler has no way to know that since all you've told the compiler is that offer will contain an OfferType. It has no way to know the specific OfferType that will be contained there.



You have to cast offer to the type you expect. I suggest you look into as and is. Here is an example:



if let discount = newBox.offer as? Discount {
print("Discount Quantity is (discount.quantity)")
}


Here the as? keyword basically tells the compiler the following:




is newBox.offer a kind of Discount? If so return that, otherwise,
return nil.








share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 3 at 1:22

























answered Jan 3 at 1:17









FJ de BrienneFJ de Brienne

14810




14810













  • I tried to do that before, it's giving me error type dude.

    – Felipe Fernandes
    Jan 3 at 1:40











  • Please show how offer is defined in newBox. Also detail the error you are obtaining.

    – FJ de Brienne
    Jan 3 at 1:42











  • I edit it again

    – Felipe Fernandes
    Jan 3 at 1:42











  • last pictures I'm showing how it's defined

    – Felipe Fernandes
    Jan 3 at 1:44











  • You have to use as? not as!, otherwise an if...let isn't valid.

    – FJ de Brienne
    Jan 3 at 1:45



















  • I tried to do that before, it's giving me error type dude.

    – Felipe Fernandes
    Jan 3 at 1:40











  • Please show how offer is defined in newBox. Also detail the error you are obtaining.

    – FJ de Brienne
    Jan 3 at 1:42











  • I edit it again

    – Felipe Fernandes
    Jan 3 at 1:42











  • last pictures I'm showing how it's defined

    – Felipe Fernandes
    Jan 3 at 1:44











  • You have to use as? not as!, otherwise an if...let isn't valid.

    – FJ de Brienne
    Jan 3 at 1:45

















I tried to do that before, it's giving me error type dude.

– Felipe Fernandes
Jan 3 at 1:40





I tried to do that before, it's giving me error type dude.

– Felipe Fernandes
Jan 3 at 1:40













Please show how offer is defined in newBox. Also detail the error you are obtaining.

– FJ de Brienne
Jan 3 at 1:42





Please show how offer is defined in newBox. Also detail the error you are obtaining.

– FJ de Brienne
Jan 3 at 1:42













I edit it again

– Felipe Fernandes
Jan 3 at 1:42





I edit it again

– Felipe Fernandes
Jan 3 at 1:42













last pictures I'm showing how it's defined

– Felipe Fernandes
Jan 3 at 1:44





last pictures I'm showing how it's defined

– Felipe Fernandes
Jan 3 at 1:44













You have to use as? not as!, otherwise an if...let isn't valid.

– FJ de Brienne
Jan 3 at 1:45





You have to use as? not as!, otherwise an if...let isn't valid.

– FJ de Brienne
Jan 3 at 1:45




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54015051%2fhow-can-i-access-properties-from-my-subclasses%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

SQL update select statement

WPF add header to Image with URL pettitions [duplicate]