Swift: automatic synthesized initializer for class (as for structs) [duplicate]












0















This question already has an answer here:




  • Why doesn't Swift provide classes memberwise initializers? [closed]

    1 answer



  • How to automatically create an initializer for a Swift class?

    2 answers




I have a struct:



struct MenuSection {
var title: String?
var items: [MenuItem] =
}


I can use the automatically generated initializer as follows:



MenuSection(title: nil, items: items)


When I change the struct to be a class, I have to write a boilerplate initializer to suppress warnings:



class MenuSection {
var title: String?
var items: [MenuItem] =

init(title: String? = nil, items: [MenuItem] = ) {
self.title = title
self.items = items
}
}


Can I force compiler to synthesize initializer in class as in struct?










share|improve this question













marked as duplicate by Martin R swift
Users with the  swift badge can single-handedly close swift questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 11 at 20:27


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1




    Short answer: No. Only structs have a default memberwise initializer.
    – Martin R
    Nov 11 at 20:29
















0















This question already has an answer here:




  • Why doesn't Swift provide classes memberwise initializers? [closed]

    1 answer



  • How to automatically create an initializer for a Swift class?

    2 answers




I have a struct:



struct MenuSection {
var title: String?
var items: [MenuItem] =
}


I can use the automatically generated initializer as follows:



MenuSection(title: nil, items: items)


When I change the struct to be a class, I have to write a boilerplate initializer to suppress warnings:



class MenuSection {
var title: String?
var items: [MenuItem] =

init(title: String? = nil, items: [MenuItem] = ) {
self.title = title
self.items = items
}
}


Can I force compiler to synthesize initializer in class as in struct?










share|improve this question













marked as duplicate by Martin R swift
Users with the  swift badge can single-handedly close swift questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 11 at 20:27


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1




    Short answer: No. Only structs have a default memberwise initializer.
    – Martin R
    Nov 11 at 20:29














0












0








0








This question already has an answer here:




  • Why doesn't Swift provide classes memberwise initializers? [closed]

    1 answer



  • How to automatically create an initializer for a Swift class?

    2 answers




I have a struct:



struct MenuSection {
var title: String?
var items: [MenuItem] =
}


I can use the automatically generated initializer as follows:



MenuSection(title: nil, items: items)


When I change the struct to be a class, I have to write a boilerplate initializer to suppress warnings:



class MenuSection {
var title: String?
var items: [MenuItem] =

init(title: String? = nil, items: [MenuItem] = ) {
self.title = title
self.items = items
}
}


Can I force compiler to synthesize initializer in class as in struct?










share|improve this question














This question already has an answer here:




  • Why doesn't Swift provide classes memberwise initializers? [closed]

    1 answer



  • How to automatically create an initializer for a Swift class?

    2 answers




I have a struct:



struct MenuSection {
var title: String?
var items: [MenuItem] =
}


I can use the automatically generated initializer as follows:



MenuSection(title: nil, items: items)


When I change the struct to be a class, I have to write a boilerplate initializer to suppress warnings:



class MenuSection {
var title: String?
var items: [MenuItem] =

init(title: String? = nil, items: [MenuItem] = ) {
self.title = title
self.items = items
}
}


Can I force compiler to synthesize initializer in class as in struct?





This question already has an answer here:




  • Why doesn't Swift provide classes memberwise initializers? [closed]

    1 answer



  • How to automatically create an initializer for a Swift class?

    2 answers








swift class struct code-generation initializer






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 20:21









Richard Topchiy

97231137




97231137




marked as duplicate by Martin R swift
Users with the  swift badge can single-handedly close swift questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 11 at 20:27


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Martin R swift
Users with the  swift badge can single-handedly close swift questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 11 at 20:27


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1




    Short answer: No. Only structs have a default memberwise initializer.
    – Martin R
    Nov 11 at 20:29














  • 1




    Short answer: No. Only structs have a default memberwise initializer.
    – Martin R
    Nov 11 at 20:29








1




1




Short answer: No. Only structs have a default memberwise initializer.
– Martin R
Nov 11 at 20:29




Short answer: No. Only structs have a default memberwise initializer.
– Martin R
Nov 11 at 20:29

















active

oldest

votes






















active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes

這個網誌中的熱門文章

Academy of Television Arts & Sciences

L'Équipe

1995 France bombings