Constructors in Swift?
up vote
1
down vote
favorite
In Java we can use constructors in order to pass initial values into a class. Is this possible in swift?
For example, in the line below I am trying to add an object, which should include all the values you can see in the function that is within it, into an array called arrayOfMedia.
self.arrayOfMedia.append(Media().getUsersMedia(image: image!, postNum: anyPosts.key, userID: user))
I cannot do this however and get the error below.
Cannot convert value of type '()' to expected argument type 'Media'
arrays swift
|
show 1 more comment
up vote
1
down vote
favorite
In Java we can use constructors in order to pass initial values into a class. Is this possible in swift?
For example, in the line below I am trying to add an object, which should include all the values you can see in the function that is within it, into an array called arrayOfMedia.
self.arrayOfMedia.append(Media().getUsersMedia(image: image!, postNum: anyPosts.key, userID: user))
I cannot do this however and get the error below.
Cannot convert value of type '()' to expected argument type 'Media'
arrays swift
Yes, they are called initialisers, the problem you have is, in you example,Media
doesn't seem to take any values via its initialiser, just like in Java, if the class's constructor doesn't provide the functionality there's nothing you can do. The reason you are likely getting the error is,getUsersMedia
is probably retuningvoid
, hence you can't add it to the array
– MadProgrammer
Nov 8 at 23:11
I'm not familiar withMedia
, but are you asking if you can create aconvenience
initializer?
– dfd
Nov 8 at 23:12
Can you show what's returned fromgetUsersMedia
?
– sloik
Nov 8 at 23:17
getUsersMedia returns nothing I was using it to set the values inside of teh class. @MadProgrammer check teh update to question
– nebur RB
Nov 8 at 23:25
"I do not want to have to initialize the class as the values going into it vary (sometimes a video and sometimes a image)" ... okay, so that makes no sense, you have a class which is a container for some type of data, why not supply two different initialisers for the two different use cases ... just like you would in Java?
– MadProgrammer
Nov 8 at 23:28
|
show 1 more comment
up vote
1
down vote
favorite
up vote
1
down vote
favorite
In Java we can use constructors in order to pass initial values into a class. Is this possible in swift?
For example, in the line below I am trying to add an object, which should include all the values you can see in the function that is within it, into an array called arrayOfMedia.
self.arrayOfMedia.append(Media().getUsersMedia(image: image!, postNum: anyPosts.key, userID: user))
I cannot do this however and get the error below.
Cannot convert value of type '()' to expected argument type 'Media'
arrays swift
In Java we can use constructors in order to pass initial values into a class. Is this possible in swift?
For example, in the line below I am trying to add an object, which should include all the values you can see in the function that is within it, into an array called arrayOfMedia.
self.arrayOfMedia.append(Media().getUsersMedia(image: image!, postNum: anyPosts.key, userID: user))
I cannot do this however and get the error below.
Cannot convert value of type '()' to expected argument type 'Media'
arrays swift
arrays swift
edited Nov 19 at 1:43
asked Nov 8 at 23:07
nebur RB
378
378
Yes, they are called initialisers, the problem you have is, in you example,Media
doesn't seem to take any values via its initialiser, just like in Java, if the class's constructor doesn't provide the functionality there's nothing you can do. The reason you are likely getting the error is,getUsersMedia
is probably retuningvoid
, hence you can't add it to the array
– MadProgrammer
Nov 8 at 23:11
I'm not familiar withMedia
, but are you asking if you can create aconvenience
initializer?
– dfd
Nov 8 at 23:12
Can you show what's returned fromgetUsersMedia
?
– sloik
Nov 8 at 23:17
getUsersMedia returns nothing I was using it to set the values inside of teh class. @MadProgrammer check teh update to question
– nebur RB
Nov 8 at 23:25
"I do not want to have to initialize the class as the values going into it vary (sometimes a video and sometimes a image)" ... okay, so that makes no sense, you have a class which is a container for some type of data, why not supply two different initialisers for the two different use cases ... just like you would in Java?
– MadProgrammer
Nov 8 at 23:28
|
show 1 more comment
Yes, they are called initialisers, the problem you have is, in you example,Media
doesn't seem to take any values via its initialiser, just like in Java, if the class's constructor doesn't provide the functionality there's nothing you can do. The reason you are likely getting the error is,getUsersMedia
is probably retuningvoid
, hence you can't add it to the array
– MadProgrammer
Nov 8 at 23:11
I'm not familiar withMedia
, but are you asking if you can create aconvenience
initializer?
– dfd
Nov 8 at 23:12
Can you show what's returned fromgetUsersMedia
?
– sloik
Nov 8 at 23:17
getUsersMedia returns nothing I was using it to set the values inside of teh class. @MadProgrammer check teh update to question
– nebur RB
Nov 8 at 23:25
"I do not want to have to initialize the class as the values going into it vary (sometimes a video and sometimes a image)" ... okay, so that makes no sense, you have a class which is a container for some type of data, why not supply two different initialisers for the two different use cases ... just like you would in Java?
– MadProgrammer
Nov 8 at 23:28
Yes, they are called initialisers, the problem you have is, in you example,
Media
doesn't seem to take any values via its initialiser, just like in Java, if the class's constructor doesn't provide the functionality there's nothing you can do. The reason you are likely getting the error is, getUsersMedia
is probably retuning void
, hence you can't add it to the array– MadProgrammer
Nov 8 at 23:11
Yes, they are called initialisers, the problem you have is, in you example,
Media
doesn't seem to take any values via its initialiser, just like in Java, if the class's constructor doesn't provide the functionality there's nothing you can do. The reason you are likely getting the error is, getUsersMedia
is probably retuning void
, hence you can't add it to the array– MadProgrammer
Nov 8 at 23:11
I'm not familiar with
Media
, but are you asking if you can create a convenience
initializer?– dfd
Nov 8 at 23:12
I'm not familiar with
Media
, but are you asking if you can create a convenience
initializer?– dfd
Nov 8 at 23:12
Can you show what's returned from
getUsersMedia
?– sloik
Nov 8 at 23:17
Can you show what's returned from
getUsersMedia
?– sloik
Nov 8 at 23:17
getUsersMedia returns nothing I was using it to set the values inside of teh class. @MadProgrammer check teh update to question
– nebur RB
Nov 8 at 23:25
getUsersMedia returns nothing I was using it to set the values inside of teh class. @MadProgrammer check teh update to question
– nebur RB
Nov 8 at 23:25
"I do not want to have to initialize the class as the values going into it vary (sometimes a video and sometimes a image)" ... okay, so that makes no sense, you have a class which is a container for some type of data, why not supply two different initialisers for the two different use cases ... just like you would in Java?
– MadProgrammer
Nov 8 at 23:28
"I do not want to have to initialize the class as the values going into it vary (sometimes a video and sometimes a image)" ... okay, so that makes no sense, you have a class which is a container for some type of data, why not supply two different initialisers for the two different use cases ... just like you would in Java?
– MadProgrammer
Nov 8 at 23:28
|
show 1 more comment
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
I do not want to have to initialize the class as the values going into it vary (sometimes a video and sometimes a image)
Okay, so that makes no sense, you have a class
which is a container for some data, some of which is optional (either you have an image or a video), why not supply two different initialisers for the two different use cases ... just like you would in Java?
There's a few ways you "might" achieve this, this is just one...
class Media {
var image: UIImage?
var video: Data?
let postKey: Int
let userId: Int
internal required init(postKey: Int, userId: Int) {
self.postKey = postKey
self.userId = userId
}
convenience init(image: UIImage, postKey: Int, userId: Int) {
self.init(postKey: postKey, userId: userId)
self.image = image
}
convenience init(video: Data, postKey: Int, userId: Int) {
self.init(postKey: postKey, userId: userId)
self.video = video
}
}
Also, note, you could have simply provided a single initialiser, something like...
init(image: UIImage? = nil, video: Data? = nil, postKey: Int, userId: Int) {...}
but this doesn't constraint the user to one or the other type (they can still pass nil
for both values)
Another approach might be to make use of a protocol
to describe the basic/common properties of Media
and then implement the different requirements (directly as struct
s or class
s or indirectly as additional protocols
)
For example...
protocol Media {
var postKey: Int { get }
var userId: Int { get }
}
struct VideoMedia: Media {
let postKey: Int
let userId: Int
let video: Data
}
struct ImageMedia: Media {
let postKey: Int
let userId: Int
let image: UIImage
}
1
SO I have been making these changes to my app, but got to a point where I need to access the #image of a certain image so I can add that to what will be added tp the DB namely a jason like image1: URL... but with these init's I cannot as when I call teh Media class it asks to be initialize. Whats a fix to this?
– nebur RB
Nov 9 at 0:36
If you have a reference to theMedia
object, why not reference theimage
property? Or are you trying to decode the JSON? I'm not sure I understand the problem you've gotten yourself into. The example above "assumes" you will have to populate the object with the data when it's initialised (ie, you can't have an emptyMedia
class). Oh and by the way - I'm "assuming" the data types, as I have zero context of the actual data you are trying to capture
– MadProgrammer
Nov 9 at 0:40
I will add new question that adresses this as I think it is not good here or do you think otherwise?
– nebur RB
Nov 9 at 0:43
1
@neburRB I'd more context before I could provide more help, so, yeah, a new question might be helpful
– MadProgrammer
Nov 9 at 0:57
stackoverflow.com/questions/53234459/… Here is teh question
– nebur RB
Nov 9 at 23:16
add a comment |
up vote
1
down vote
For what it's worth, in this case I would use enums to wrap your media types.
enum MediaType {
case image(UIImage)
case video(Data)
}
Then you have Type safe access without requiring optionals:
struct Media {
let postKey: Int
let userId: Int
let mediaType: MediaType
}
let video = Media(postKey: 1, userId: 2, mediaType: .video(dataVariable))
let image = Media(postKey: 2, userId: 3, mediaType: .image(imageVariable))
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
I do not want to have to initialize the class as the values going into it vary (sometimes a video and sometimes a image)
Okay, so that makes no sense, you have a class
which is a container for some data, some of which is optional (either you have an image or a video), why not supply two different initialisers for the two different use cases ... just like you would in Java?
There's a few ways you "might" achieve this, this is just one...
class Media {
var image: UIImage?
var video: Data?
let postKey: Int
let userId: Int
internal required init(postKey: Int, userId: Int) {
self.postKey = postKey
self.userId = userId
}
convenience init(image: UIImage, postKey: Int, userId: Int) {
self.init(postKey: postKey, userId: userId)
self.image = image
}
convenience init(video: Data, postKey: Int, userId: Int) {
self.init(postKey: postKey, userId: userId)
self.video = video
}
}
Also, note, you could have simply provided a single initialiser, something like...
init(image: UIImage? = nil, video: Data? = nil, postKey: Int, userId: Int) {...}
but this doesn't constraint the user to one or the other type (they can still pass nil
for both values)
Another approach might be to make use of a protocol
to describe the basic/common properties of Media
and then implement the different requirements (directly as struct
s or class
s or indirectly as additional protocols
)
For example...
protocol Media {
var postKey: Int { get }
var userId: Int { get }
}
struct VideoMedia: Media {
let postKey: Int
let userId: Int
let video: Data
}
struct ImageMedia: Media {
let postKey: Int
let userId: Int
let image: UIImage
}
1
SO I have been making these changes to my app, but got to a point where I need to access the #image of a certain image so I can add that to what will be added tp the DB namely a jason like image1: URL... but with these init's I cannot as when I call teh Media class it asks to be initialize. Whats a fix to this?
– nebur RB
Nov 9 at 0:36
If you have a reference to theMedia
object, why not reference theimage
property? Or are you trying to decode the JSON? I'm not sure I understand the problem you've gotten yourself into. The example above "assumes" you will have to populate the object with the data when it's initialised (ie, you can't have an emptyMedia
class). Oh and by the way - I'm "assuming" the data types, as I have zero context of the actual data you are trying to capture
– MadProgrammer
Nov 9 at 0:40
I will add new question that adresses this as I think it is not good here or do you think otherwise?
– nebur RB
Nov 9 at 0:43
1
@neburRB I'd more context before I could provide more help, so, yeah, a new question might be helpful
– MadProgrammer
Nov 9 at 0:57
stackoverflow.com/questions/53234459/… Here is teh question
– nebur RB
Nov 9 at 23:16
add a comment |
up vote
3
down vote
accepted
I do not want to have to initialize the class as the values going into it vary (sometimes a video and sometimes a image)
Okay, so that makes no sense, you have a class
which is a container for some data, some of which is optional (either you have an image or a video), why not supply two different initialisers for the two different use cases ... just like you would in Java?
There's a few ways you "might" achieve this, this is just one...
class Media {
var image: UIImage?
var video: Data?
let postKey: Int
let userId: Int
internal required init(postKey: Int, userId: Int) {
self.postKey = postKey
self.userId = userId
}
convenience init(image: UIImage, postKey: Int, userId: Int) {
self.init(postKey: postKey, userId: userId)
self.image = image
}
convenience init(video: Data, postKey: Int, userId: Int) {
self.init(postKey: postKey, userId: userId)
self.video = video
}
}
Also, note, you could have simply provided a single initialiser, something like...
init(image: UIImage? = nil, video: Data? = nil, postKey: Int, userId: Int) {...}
but this doesn't constraint the user to one or the other type (they can still pass nil
for both values)
Another approach might be to make use of a protocol
to describe the basic/common properties of Media
and then implement the different requirements (directly as struct
s or class
s or indirectly as additional protocols
)
For example...
protocol Media {
var postKey: Int { get }
var userId: Int { get }
}
struct VideoMedia: Media {
let postKey: Int
let userId: Int
let video: Data
}
struct ImageMedia: Media {
let postKey: Int
let userId: Int
let image: UIImage
}
1
SO I have been making these changes to my app, but got to a point where I need to access the #image of a certain image so I can add that to what will be added tp the DB namely a jason like image1: URL... but with these init's I cannot as when I call teh Media class it asks to be initialize. Whats a fix to this?
– nebur RB
Nov 9 at 0:36
If you have a reference to theMedia
object, why not reference theimage
property? Or are you trying to decode the JSON? I'm not sure I understand the problem you've gotten yourself into. The example above "assumes" you will have to populate the object with the data when it's initialised (ie, you can't have an emptyMedia
class). Oh and by the way - I'm "assuming" the data types, as I have zero context of the actual data you are trying to capture
– MadProgrammer
Nov 9 at 0:40
I will add new question that adresses this as I think it is not good here or do you think otherwise?
– nebur RB
Nov 9 at 0:43
1
@neburRB I'd more context before I could provide more help, so, yeah, a new question might be helpful
– MadProgrammer
Nov 9 at 0:57
stackoverflow.com/questions/53234459/… Here is teh question
– nebur RB
Nov 9 at 23:16
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
I do not want to have to initialize the class as the values going into it vary (sometimes a video and sometimes a image)
Okay, so that makes no sense, you have a class
which is a container for some data, some of which is optional (either you have an image or a video), why not supply two different initialisers for the two different use cases ... just like you would in Java?
There's a few ways you "might" achieve this, this is just one...
class Media {
var image: UIImage?
var video: Data?
let postKey: Int
let userId: Int
internal required init(postKey: Int, userId: Int) {
self.postKey = postKey
self.userId = userId
}
convenience init(image: UIImage, postKey: Int, userId: Int) {
self.init(postKey: postKey, userId: userId)
self.image = image
}
convenience init(video: Data, postKey: Int, userId: Int) {
self.init(postKey: postKey, userId: userId)
self.video = video
}
}
Also, note, you could have simply provided a single initialiser, something like...
init(image: UIImage? = nil, video: Data? = nil, postKey: Int, userId: Int) {...}
but this doesn't constraint the user to one or the other type (they can still pass nil
for both values)
Another approach might be to make use of a protocol
to describe the basic/common properties of Media
and then implement the different requirements (directly as struct
s or class
s or indirectly as additional protocols
)
For example...
protocol Media {
var postKey: Int { get }
var userId: Int { get }
}
struct VideoMedia: Media {
let postKey: Int
let userId: Int
let video: Data
}
struct ImageMedia: Media {
let postKey: Int
let userId: Int
let image: UIImage
}
I do not want to have to initialize the class as the values going into it vary (sometimes a video and sometimes a image)
Okay, so that makes no sense, you have a class
which is a container for some data, some of which is optional (either you have an image or a video), why not supply two different initialisers for the two different use cases ... just like you would in Java?
There's a few ways you "might" achieve this, this is just one...
class Media {
var image: UIImage?
var video: Data?
let postKey: Int
let userId: Int
internal required init(postKey: Int, userId: Int) {
self.postKey = postKey
self.userId = userId
}
convenience init(image: UIImage, postKey: Int, userId: Int) {
self.init(postKey: postKey, userId: userId)
self.image = image
}
convenience init(video: Data, postKey: Int, userId: Int) {
self.init(postKey: postKey, userId: userId)
self.video = video
}
}
Also, note, you could have simply provided a single initialiser, something like...
init(image: UIImage? = nil, video: Data? = nil, postKey: Int, userId: Int) {...}
but this doesn't constraint the user to one or the other type (they can still pass nil
for both values)
Another approach might be to make use of a protocol
to describe the basic/common properties of Media
and then implement the different requirements (directly as struct
s or class
s or indirectly as additional protocols
)
For example...
protocol Media {
var postKey: Int { get }
var userId: Int { get }
}
struct VideoMedia: Media {
let postKey: Int
let userId: Int
let video: Data
}
struct ImageMedia: Media {
let postKey: Int
let userId: Int
let image: UIImage
}
edited Nov 9 at 0:27
answered Nov 9 at 0:21
MadProgrammer
297k17152263
297k17152263
1
SO I have been making these changes to my app, but got to a point where I need to access the #image of a certain image so I can add that to what will be added tp the DB namely a jason like image1: URL... but with these init's I cannot as when I call teh Media class it asks to be initialize. Whats a fix to this?
– nebur RB
Nov 9 at 0:36
If you have a reference to theMedia
object, why not reference theimage
property? Or are you trying to decode the JSON? I'm not sure I understand the problem you've gotten yourself into. The example above "assumes" you will have to populate the object with the data when it's initialised (ie, you can't have an emptyMedia
class). Oh and by the way - I'm "assuming" the data types, as I have zero context of the actual data you are trying to capture
– MadProgrammer
Nov 9 at 0:40
I will add new question that adresses this as I think it is not good here or do you think otherwise?
– nebur RB
Nov 9 at 0:43
1
@neburRB I'd more context before I could provide more help, so, yeah, a new question might be helpful
– MadProgrammer
Nov 9 at 0:57
stackoverflow.com/questions/53234459/… Here is teh question
– nebur RB
Nov 9 at 23:16
add a comment |
1
SO I have been making these changes to my app, but got to a point where I need to access the #image of a certain image so I can add that to what will be added tp the DB namely a jason like image1: URL... but with these init's I cannot as when I call teh Media class it asks to be initialize. Whats a fix to this?
– nebur RB
Nov 9 at 0:36
If you have a reference to theMedia
object, why not reference theimage
property? Or are you trying to decode the JSON? I'm not sure I understand the problem you've gotten yourself into. The example above "assumes" you will have to populate the object with the data when it's initialised (ie, you can't have an emptyMedia
class). Oh and by the way - I'm "assuming" the data types, as I have zero context of the actual data you are trying to capture
– MadProgrammer
Nov 9 at 0:40
I will add new question that adresses this as I think it is not good here or do you think otherwise?
– nebur RB
Nov 9 at 0:43
1
@neburRB I'd more context before I could provide more help, so, yeah, a new question might be helpful
– MadProgrammer
Nov 9 at 0:57
stackoverflow.com/questions/53234459/… Here is teh question
– nebur RB
Nov 9 at 23:16
1
1
SO I have been making these changes to my app, but got to a point where I need to access the #image of a certain image so I can add that to what will be added tp the DB namely a jason like image1: URL... but with these init's I cannot as when I call teh Media class it asks to be initialize. Whats a fix to this?
– nebur RB
Nov 9 at 0:36
SO I have been making these changes to my app, but got to a point where I need to access the #image of a certain image so I can add that to what will be added tp the DB namely a jason like image1: URL... but with these init's I cannot as when I call teh Media class it asks to be initialize. Whats a fix to this?
– nebur RB
Nov 9 at 0:36
If you have a reference to the
Media
object, why not reference the image
property? Or are you trying to decode the JSON? I'm not sure I understand the problem you've gotten yourself into. The example above "assumes" you will have to populate the object with the data when it's initialised (ie, you can't have an empty Media
class). Oh and by the way - I'm "assuming" the data types, as I have zero context of the actual data you are trying to capture– MadProgrammer
Nov 9 at 0:40
If you have a reference to the
Media
object, why not reference the image
property? Or are you trying to decode the JSON? I'm not sure I understand the problem you've gotten yourself into. The example above "assumes" you will have to populate the object with the data when it's initialised (ie, you can't have an empty Media
class). Oh and by the way - I'm "assuming" the data types, as I have zero context of the actual data you are trying to capture– MadProgrammer
Nov 9 at 0:40
I will add new question that adresses this as I think it is not good here or do you think otherwise?
– nebur RB
Nov 9 at 0:43
I will add new question that adresses this as I think it is not good here or do you think otherwise?
– nebur RB
Nov 9 at 0:43
1
1
@neburRB I'd more context before I could provide more help, so, yeah, a new question might be helpful
– MadProgrammer
Nov 9 at 0:57
@neburRB I'd more context before I could provide more help, so, yeah, a new question might be helpful
– MadProgrammer
Nov 9 at 0:57
stackoverflow.com/questions/53234459/… Here is teh question
– nebur RB
Nov 9 at 23:16
stackoverflow.com/questions/53234459/… Here is teh question
– nebur RB
Nov 9 at 23:16
add a comment |
up vote
1
down vote
For what it's worth, in this case I would use enums to wrap your media types.
enum MediaType {
case image(UIImage)
case video(Data)
}
Then you have Type safe access without requiring optionals:
struct Media {
let postKey: Int
let userId: Int
let mediaType: MediaType
}
let video = Media(postKey: 1, userId: 2, mediaType: .video(dataVariable))
let image = Media(postKey: 2, userId: 3, mediaType: .image(imageVariable))
add a comment |
up vote
1
down vote
For what it's worth, in this case I would use enums to wrap your media types.
enum MediaType {
case image(UIImage)
case video(Data)
}
Then you have Type safe access without requiring optionals:
struct Media {
let postKey: Int
let userId: Int
let mediaType: MediaType
}
let video = Media(postKey: 1, userId: 2, mediaType: .video(dataVariable))
let image = Media(postKey: 2, userId: 3, mediaType: .image(imageVariable))
add a comment |
up vote
1
down vote
up vote
1
down vote
For what it's worth, in this case I would use enums to wrap your media types.
enum MediaType {
case image(UIImage)
case video(Data)
}
Then you have Type safe access without requiring optionals:
struct Media {
let postKey: Int
let userId: Int
let mediaType: MediaType
}
let video = Media(postKey: 1, userId: 2, mediaType: .video(dataVariable))
let image = Media(postKey: 2, userId: 3, mediaType: .image(imageVariable))
For what it's worth, in this case I would use enums to wrap your media types.
enum MediaType {
case image(UIImage)
case video(Data)
}
Then you have Type safe access without requiring optionals:
struct Media {
let postKey: Int
let userId: Int
let mediaType: MediaType
}
let video = Media(postKey: 1, userId: 2, mediaType: .video(dataVariable))
let image = Media(postKey: 2, userId: 3, mediaType: .image(imageVariable))
answered Nov 9 at 0:49
PeejWeej
4,88111438
4,88111438
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53217525%2fconstructors-in-swift%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Yes, they are called initialisers, the problem you have is, in you example,
Media
doesn't seem to take any values via its initialiser, just like in Java, if the class's constructor doesn't provide the functionality there's nothing you can do. The reason you are likely getting the error is,getUsersMedia
is probably retuningvoid
, hence you can't add it to the array– MadProgrammer
Nov 8 at 23:11
I'm not familiar with
Media
, but are you asking if you can create aconvenience
initializer?– dfd
Nov 8 at 23:12
Can you show what's returned from
getUsersMedia
?– sloik
Nov 8 at 23:17
getUsersMedia returns nothing I was using it to set the values inside of teh class. @MadProgrammer check teh update to question
– nebur RB
Nov 8 at 23:25
"I do not want to have to initialize the class as the values going into it vary (sometimes a video and sometimes a image)" ... okay, so that makes no sense, you have a class which is a container for some type of data, why not supply two different initialisers for the two different use cases ... just like you would in Java?
– MadProgrammer
Nov 8 at 23:28