Public Bool Get Set Statement - boolean

By all means this statement should work, but returns and error when I compile? Thoughts?
public bool sheetCreatorSave {
get {
return (bool)this["sheetCreatorSave"];
}
set {
this["sheetCreatorSave"] = (object)(bool)(value ? 1 : 0);
}
}

Related

Why ReturnType is not working in this Swift function?

I am working with Swift 5, in Xcode 10.2.1
I have this function inside of an extension of UInt8
The compiler complains in line 5, with Unexpected non-void return value in void function
The return type is properly defined, and if the line return "\(opCode)" is commented, it works fine, with the return in the last line return "\(OpCode.NOP) I am using "\(OpCode.NOP)" to avoid adding another variable to the enum, but is a regular string
The error continues if I change the line to return "", so it has nothing to do with the OpCode enum.
extension UInt8 {
func opCode() -> String {
OpCode.allCases.forEach { opCode in
if self == opCode.uint8 {
return "\(opCode)" //Unexpected non-void return value in void function
//return "" // Error persists
}
}
return "\(OpCode.NOP)"
}
}
You can't return a value inside void return type of the forEach instead try
extension UInt8 {
func opCode() -> String {
if let item = OpCode.allCases.first(where:{ self == $0.uint8 }) {
return "\(item)"
}
return "\(OpCode.NOP)"
}
}

Can not call value of non function type

I am getting error can not call value of non function type. Whats wrong in this ?
extension FileModel {
var isPlayableMedia: Bool {
get {
return isPlayableMedia(mediaType: mediaType)
}
}
func isPlayableMedia(_ mediaType: MediaType) -> Bool {
return mediaType == MediaType.image || mediaType == MediaType.video
}
}
Either remove the label parameter when the method is called
var isPlayableMedia: Bool {
return isPlayableMedia(mediaType) // assuming `mediaType` is declared somewhere else.
}
Or specify the label parameter in the declaration (remove the underscore)
func isPlayableMedia(mediaType: MediaType) -> Bool {
return mediaType == MediaType.image || mediaType == MediaType.video
}
Nothing much just check your function signature and use it in right way.
replace your var with the following code :
var isPlayableMedia: Bool {
get {
return isPlayableMedia(mediaType)
}
}
OR
just remove the argument label from you method calling

calling functions on subscript returnvalue

Im experimenting with subscripts and generics, and what I am trying to do is this:
heap[0]!.hasLeftChild
Where my heap class looks something like this:
public class Heap<T> where T:HeapSubscriptable {
private var size = Int()
private var valueList = [T]()
public var capacity = 3
// subscript setter
subscript(i: Int) -> T? {
if i < self.size {
return valueList[i]
} else {
return nil
}
}
...
}
To allow this i made a protocol extension:
public protocol HeapSubscriptable {
var hasLeftChild: Bool{ get }
}
extension HeapSubscriptable {
public var hasLeftChild: Bool {
// return (self.size > 1+i*2 ) // <-- I want to get a hold of this i
}
}
extension Int : HeapSubscriptable{}
But to calculate this I need access to the subscript parameter i, so that i can use its index in my array and do magic on it and see if it actually has a left child. Is there away to access it?

Swift: Array contains(AnyObject) error Cannot convert type of ... throw -> Bool

Why I use Array.contains(AnyObject) to checking whether an structure object exists in this Array. It makes error : "Cannot convert type of ... throw -> Bool"
struct DecorationPatternsData {
let patternImageName: String
init(patternImageName: String) {
self.patternImageName = patternImageName
}
}
var decorationPatterns : [DecorationPatternsData] = [DecorationPatternsData(patternImageName: "decoration1.gif"), DecorationPatternsData(patternImageName: "decoration1.gif"), DecorationPatternsData(patternImageName: "decoration1.gif")]
var pickedDecorationPattern : DecorationPatternsData? = nil
...
if (pickedDecorationPattern != nil) {
if (decorationPatterns.contains(pickedDecorationPattern)) {
// Error: Cannot convert type of ... throw -> Bool
}
}
That's because your DecorationPatternsData doesn't conform to Equatable which is a requirement for contains(_:) to work.
Solution 1:
extension DecorationPatternsData: Equatable { }
func ==(lhs: DecorationPatternsData, rhs: DecorationPatternsData) -> Bool {
return lhs.patternImageName == rhs.patternImageName
}
Now DecorationPatternsData conforms to Equatable so you can use:
if let pickedDecorationPattern = pickedDecorationPattern {
if decorationPatterns.contains(pickedDecorationPattern) {
// Your code
}
}
Solution 2:
if decorationPatterns.contains({ $0 == pickedDecorationPattern }) {
// Your code
}
Here you're using a closure to compare the elements, returning a bool for contains(_:)
...
if let pickedDecorationPattern = pickedDecorationPattern {
if decorationPatterns.contains(pickedDecorationPattern) {
// Error: Cannot convert type of ... throw -> Bool
}
}

Can you get __FUNCTION__ from the caller?

Example:
#noreturn func setOnlyPropertyGetterError(__function__: String) {
fatalError("\(__function__) is set-only")
}
var property: Property {
get {setOnlyPropertyGetterError(__FUNCTION__)}
set {//useful work}
}
Can we avoid having to pass __FUNCTION__?
I think this is what you want to achieve:
#noreturn func writeOnlyProperty(propertyName: String = __FUNCTION__) {
fatalError("Property \(propertyName) is write-only")
}
class Foo {
var blackHole: Int {
get { writeOnlyProperty() }
set { print("Consuming value \(newValue)") }
}
}
let foo = Foo()
foo.blackHole = 1 // Prints "Consuming value 1"
let bar = foo.blackHole // Produces fatal error "Property blackHole is write-only"