Segmentation Fault in Swift Playground - swift

I'm new here and I'm doing some exercise in Swift and I found this problem:
When I try to execute this code I get a Segmentation Fault and I can't figure out why.
class Persona{
private var nome:String?
private var cognome:String?
private var eta:Int?
public var Sesso:String{
get{
return Sesso
}
set{
if newValue=="M" || newValue == "m" {
Sesso="Maschio"
}
else{
Sesso="Femmina"
}
}
}
init(nome:String, cognome:String, eta:Int)
{
self.nome=nome
self.cognome=cognome
self.eta=eta
}
init(){}
func getNome()->String{ return nome! }
func getCognome()->String{ return cognome! }
func getEta()->Int{ return eta! }
func setNome(nome:String)->(){ self.nome=nome }
func setCognome(cognome:String)->(){ self.cognome=cognome }
func setEta(eta:Int)->(){ self.eta=eta }
}
var p1 = Persona(nome:"AAAA",cognome:"BBBB",eta:22)
p1.Sesso = "M"
p1.setEta(eta:44)
print("\(p1.getNome()) \t \(p1.getCognome()) \t \(p1.getEta()) \t \(p1.Sesso)")
p1.setEta(eta:22)

You are basically trying to use a computed property
In addition to stored properties, classes, structures, and enumerations can define computed properties, which do not actually store a value. Instead, they provide a getter and an optional setter to retrieve and set other properties and values indirectly.
The key here is "which do not actually store a value". That can be seen clearer when you try to use the getter since print(p1.Sesso) causes a crash as well since you end up in an infinite loop of the getter calling itself over and over again. What you should do is define the computed property but at the same time define another stored property which actually holds the data:
private var _sesso:String = ""
public var Sesso:String{
get{
return _sesso
}
set{
if newValue=="M" || newValue == "m" {
_sesso="Maschio"
}
else{
_sesso="Femmina"
}
}
}

The Problem is you can't access a variable in its own getter and setter.
Instead you can use didSet
public var Sesso : String{
didSet {
if Sesso == "M" || Sesso == "m" {
Sesso = "Maschio"
}
else{
Sesso = "Femmina"
}
}
}
and you can know more about getters and setters from this answer and can also check apple documentation on Properties for more clarification.

Related

Executiong closure on array modification

I have the following code:
class Note: NSObject {
}
struct Global {
static var notes: Array<Note> = [] {
didSet {
print("hi")
}
}
}
This prints "hi" if I add or remove an item from the array or if I do
Global.notes = []
Is there a way to print("hi") every time when one of the Note objects in the array is modified?
Thanks for your answers
Without changing the class to a struct, I have two basic ways to handle this.
This is the object you asked about
class Note: NSObject {
}
struct Global {
static var notes: Array<Note> = [] {
didSet {
print("hi")
}
}
}
Wrap Notes in a wrapper that is a struct to get the struct behavior.
extension Note {
struct Wrapper { let note: Note }
}
extension Global {
static var wrappedNotes = [Note.Wrapper]() {
didSet {
print("hi")
}
}
}
Global.wrappedNotes.append(Note.Wrapper(note: Note()))
Global.wrappedNotes[0] = Note.Wrapper(note: Note())
Global.wrappedNotes.remove(at: 0)
The other way is to create a note manager to wrap access to the array.
class NoteManager {
subscript(index: Int) -> Note {
get {
return values[index]
}
set {
defer { onUpdate() }
values[index] = newValue
}
}
func append(_ newNote: Note) {
defer { onUpdate() }
values.append(newNote)
}
func remove(at index: Int) -> Note {
defer { onUpdate() }
return values.remove(at: index)
}
private func onUpdate() {
print("hi")
}
private var values = [Note]()
}
extension Global {
static var managedNotes = NoteManager()
}
Global.managedNotes.append(Note())
Global.managedNotes[0] = Note()
Global.managedNotes.remove(at: 0)
As per #staticVoidMan comment , If you make your model , a struct, rather than a class, then the property observer didSet will work for your Note model's own properties as well.
import Foundation
struct Note {
var name: String
}
struct Global {
static var notes: Array<Note> = [] {
didSet {
print("hi")
}
}
}
Global.notes.append(Note(name: "Shubham"))
Global.notes.append(Note(name: "Bakshi"))
Global.notes[0].name = "Boxy"
This will print the following on the console :
hi
hi
hi
Swift Array is a struct, and structs are value-type which means they change completely when elements are added/removed/replaced. Hence when you add/remove/replace a Note, the didSet property observer gets called as the array has been set again.
However, as per you question:
Is there a way to print("hi") every time when one of the Note objects in the array is modified?
By this I am assuming that you want to do something when an element within this array is accessed and an internal property is modified.
This would have been fine if you were dealing with only value-type objects, i.e. had your Note object also been a struct, then changing anything inside one Note would have caused the array to change as well.
But your Note object is a class, i.e. reference-type, and stays as the same object even if it's internal elements change. Hence your array doesn't need to update and didSet does not get called.
Read: Value and Reference Types
KVO Solution:
Now... Since your Note is subclassing NSObject, you can use the KVO concept
As per the following working example, we observe only one property of the Note class.
If you want to observe more properties then you will need to observe those many more keypaths.
Example:
class Note: NSObject {
#objc dynamic var content = ""
init(_ content: String) {
self.content = content
}
}
class NoteList {
var notes: [Note] = [] {
didSet {
print("note list updated")
//register & save observers for each note
self.noteMessageKVOs = notes.map { (note) -> NSKeyValueObservation in
return note.observe(\Note.content, options: [.new, .old]) { (note, value) in
print("note updated: \(value.oldValue) changed to \(value.newValue)")
}
}
}
}
//array of observers
var noteMessageKVOs = [NSKeyValueObservation]()
}
let list = NoteList()
list.notes.append(Note("A")) //note list updated
list.notes.append(Note("B")) //note list updated
list.notes[0].content = "X" //note updated: A changed to X
list.notes[1].content = "Y" //note updated: B changed to Y
Notes:
NSObject is required for KVO
#objc dynamic is required to make a property observable
\Note.message is a keypath
noteMessageKVOs are required to keep the observers alive

Swift 4.2 Setter Getter, All paths through this function will call itself

With swift 4.2 I have begun to see a lot of issues, and one of them i'm not really sure how to resolve, since my getter method should be returning the value itself.
I imagine what is happening is that the getter will attempt to access the getter when calling self.type
How can i resolve this issue?
Here is a screenshot of the code with the error.
Thanks in advance
Here is the written code
#objc var type: DecisionType {
set {
if(newValue == DecisionType.DecisionDouble){
//Yes button and NO button should be showing
okButton.isHidden = true;
yesButton.isHidden = false;
noButton.isHidden = false;
}
else {
//Only Ok button should be showing
okButton.isHidden = false;
yesButton.isHidden = true;
noButton.isHidden = true;
}
}
get {
return self.type;
}
};
Your issue is that there is no stored property type for the getter to return. type is a computed property. When you try to read its value, it calls the getter you defined. This getter calls the getter, which in turn calls the getter which calls the getter... and so on. You have infinite recursion.
Most likely, what you meant to do is have a stored property, that just has some fancy behaviour whenever its set. Rather than using a computed property with a custom get and set, use a stored property with a willSet or didSet block:
#objc var type: DecisionType {
didSet {
let isDecisionDouble = newValue == DecisionType.DecisionDouble
okButton.isHidden = isDecisionDouble;
yesButton.isHidden = !isDecisionDouble;
noButton.isHidden = !isDecisionDouble;
}
}
A better approach for this case if to have one extra property which you will use it in return value for getter and you set it when your main property change.
For instance let say your main property which you use is type then have an extra property _type note the underscore next to it.
Then here is how you would set and retrieve your and set your main property type
// This is a an extra property which you will use internally
private var _type: DecisionType?
// Then use it as shown below
var type:DecisionType? {
get {
return _type
}
set {
_type = newValue
}
}
My issue was that I had get/set in the parent protocol extension rather than the protocol extension of the actual protocol:
protocol MainProtocol: SubProtocol {
var mainVariable: String { get set }
}
protocol SubProtocol {
var subVariable: String { get set }
}
extension MainProtocol {
var mainVariable { get { return self.mainVariable } set { self.mainVariable = newValue } }
//I KEPT MY VARIABLE HERE.
var subVariable { get { return self.subVariable } set { self.subVariable = newValue } }
}
extension SubProtocol {
//THIS IS WHERE THE VARIABLE SHOULD HAVE BEEN
}

Can I add associated object to Swift Struct?

I would like to add an additional property to the Swift String. I used this approach few times on objects, but it seems that it does not work on struct. Although, I don't get any error...
This is what I tried:
var str = "Hello, StackOverflow"
fileprivate struct AssociatedKeys {
static var myBool = "myBool"
}
extension String {
public var myBool: Bool {
get {
guard let myBoolObject = objc_getAssociatedObject(self, &AssociatedKeys.myBool) as? NSNumber else {
return false
}
return myBoolObject.boolValue // execution never reaches this line
}
set(value) {
let object = NSNumber(value: value)
objc_setAssociatedObject(self, &AssociatedKeys.myBool, object, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
}
str.myBool = true
print(str.myBool) // prints "false"
It prints out that it is false.
At first, I tried it without wrapping the Bool into NSNumber, but the result was the same.
Is this even possible to add an associated object to a struct at all? If not, can anyone tell me why?
Based on #Hamish's comment, I created the following solution to workaround the issue.
Preconditions:
Have a framework which proposes prefilled objects, the app works on these objects and the framework should know which of the properties are modified during the processing of this object later.
Not using looooong initializers to setup all properties of MyObject is a design decision.
In my example, the usage of the myObject is a dummy and shows what happens in the framework and what happens in the app.
// protocol is used, as we could handle more modifiable structs/classes in a common way
protocol PropertyState {
var isModified: Bool {get set}
}
internal struct ModifiableString : PropertyState {
var string: String
var isModified: Bool
}
class MyObject: PropertyState {
internal var _name = ModifiableString(string: "", isModified: false)
public var name: String {
get {
return _name.string
}
set(value) {
_name.string = value
_name.isModified = true
}
}
// + N similar properties (they can be other types than String, by implementing other structs, like ModifiableBool)
var isModified: Bool {
get {
return _name.isModified // || _myAnotherProperty.isModified
}
set(value) {
_name.isModified = value
// _myAnotherProperty.isModified = value
}
}
}
// internal filling of the object inside of the framework
let myObject = MyObject()
myObject.name = "originalValue"
print(myObject.isModified) // true
// filling the object with values ended, so we can set the state
myObject.isModified = false
print(myObject.isModified) // false
// the app can work with the object
// let myObject = Framework.getObject()
myObject.name = "modifiedValue"
// now the framework should now which properties are modified
print(myObject._name.isModified) // true

Read once and then set to nil

I'd like to implement such property, that it's value is available for reading only one time, and then the property should be set to nil.
I've implemented it in such way:
private var _readOnce: String?
var readOnce: String? {
get {
let value = _readOnce
_readOnce = nil
return value
}
set {
_readOnce = newValue
}
}
readOnce = "Andrej"
print("read once = \(readOnce)") // prints read once = Optional("Andrej")\n"
print("read once = \(readOnce)") // prints read once = nil\n"
But I'feel like using a separate property _readOnce is not the "swifty" / "most elegant" way to do it.
Does anyone know of a different way, that wouldn't require to use a separate property?
I can confirm that the above code works, it's only that I feel it could be more elegant with less lines to achieve the same behaviour.
I don't know that there's a way to avoid having a backing property, but what I'd probably do is to make a helper type to wrap up the behavior. Something like this:
struct OneTimeValue<T>
{
private var isUnread = true
private let value : T
init(_ value: T)
{
self.value = value
}
func get() -> T?
{
guard isUnread else {
return nil
}
self.isUnread = false
return self.value
}
}
You could also write this a little differently if you prefer, by nilling out value inside of get(), for example, but the general plan holds.
Then your class becomes:
class Miser
{
var readOnce : String?
{
return self._readOnce.get()
}
private let _readOnce = OneTimeValue("Can't touch this (twice)")
}
I've also used this pattern for a UserDefaultsValue (storage to/from user defaults) and a SynchronizedValue (read-write lock on a property) and I think it works well.
As far as I know it is not possible without a second variable. This is because computed properties do not store any data for the variable they represent:
In addition to stored properties, classes, structures, and
enumerations can define computed properties, which do not actually
store a value.
For non-computed properties, the only observers you can have are based upon the setting of the variable, not the getting (i.e. willSet and didSet)
Hope that helps!
EDIT:
It can be done with closures and property observers if you're careful:
This requires no other variables (instead the value is captured by the closure), but it is rather unclear — I wouldn't recommend it.
var readOnce: () -> String? = {nil} {
didSet{
readOnce = { [weak self, readOnce] in
self?.readOnce = {nil}
return readOnce()
}
}
}
readOnce() // returns nil
readOnce = {"Hi"}
readOnce() // returns optional wrapped "Hi"
readOnce() // returns nil
A more 'Swifty' answer for you :D
After Swift 5.1, We can use Property Wrapper
#propertyWrapper
struct ReturnAndFree<T> {
private var value: T?
init(wrappedValue: T?) {
value = wrappedValue
}
var wrappedValue: T? {
mutating get {
defer { value = nil }
return value
}
set {
value = newValue
}
}
}

How can I set a string variable and make it always lowercase?

I want to set a string variable and want to keep it always lowercase.
This is my code :
var alwaysLowercaseString : String? {
didSet{
alwaysLowercaseString = alwaysLowerCaseString!.lowercaseString
}
}
But when I use it, it goes into a infinite loop. How can I solve this problem?
I stand corrected, this is the correct approach. LeoDabus deserves the credit for this answer:
var alwaysLowercaseString : String? {
didSet{
alwaysLowercaseString = alwaysLowercaseString?.lowercaseString
print(alwaysLowercaseString)
}
}
var alwaysLowercaseString : String? {
didSet{
if alwaysLowercaseString != alwaysLowerCaseString!.lowercaseString {
alwaysLowercaseString = alwaysLowerCaseString!.lowercaseString
}
}
}
This checks so if the lowercase string is already lowercase it won't change the value of alwaysLowercaseString again so you won't call didSet infinitely. It will break after alwaysLowercaseString is set to lowercase.
Since Swift 5.1 there's a possibility to use property wrappers. Roughly speaking, this mechanics allows us to modify the value each time it is set
#propertyWrapper struct Lowercased {
var wrappedValue: String {
didSet { wrappedValue = wrappedValue.lowercased() } // here the magic happens
}
init(wrappedValue: String) {
self.wrappedValue = wrappedValue.lowercased() // we have to do it manually here because in init property observers are not triggered
}
}
final class Test {
#Lowercased var cantMakeMeUpperCase = ""
}
let test = Test()
test.cantMakeMeUpperCase = "CAN I?"
print(test.cantMakeMeUpperCase) // prints "can i?"