Swift Dictionary Initialization of custom type gives: '' is not a postfix unary operator error
While I can't tell you why the swift compiler emits this error, you can make it compile like this:
struct Prims {
var msSet = Dictionary<Vertex<Int>, Double>()
}
or like this:
struct Prims {
typealias V = Vertex<Int>
var msSet = [V : Double]()
}
Why can't I use the short Array constructor syntax when creating an Array of a nested Struct?
It's just a hole in the language. After all, the [Type]
syntax is just syntactic sugar; as you rightly say, if you use the real syntax with Array<Type>
, or use [Type]
but not as a constructor, there's no problem. You can also work around it with type alias:
struct Struct1 {
struct Struct2 {
var name: String?
}
}
typealias Struct2 = Struct1.Struct2
var struct2Array = [Struct2]()
How to calculate percentage using % as a postfix unary operator in Swift 3 and still be able to use % for modulo?
Just move
var percentage = 25%
under
postfix func % (percentage: Int) -> Double {
return (Double(percentage) / 100)
}
Like so
postfix operator %
postfix func % (percentage: Int) -> Double {
return (Double(percentage) / 100)
}
var percentage = 25%
Reason: your code would work in an app, but not in a playground, because a playground interprets the code from top to bottom. It doesn't see the postfix func
declaration located below var percentage
, so at the point of var percentage
it gives you an error, because it doesn't yet know what to do with it.
Weird escaping function behavior after updating to Swift 3
Looks like a bug to me. For some reason, the compiler doesn't like the syntax:
private var functionHandlers = [(@escaping () -> Int) -> ()]()
but it does like:
private var functionHandlers : [(@escaping () -> Int) -> ()] = []
It's the same symptom, but I'm unsure it's the same cause as the compiler rejecting the [TypeA.TypeB]()
syntax with nested types. Although like that problem, another way of solving it is by using a typealias
:
typealias F = (@escaping () -> Int) -> ()
private var functionHandlers = [F]()
You can then implement doRegister()
as you correctly tried to implement it as:
func doRegister() {
functionHandlers.append { (f: @escaping () -> Int) in
myFunction = f
}
}
Although you should certainly file a bug report over [(@escaping () -> Int) -> ()]()
not compiling.
unary operator expected error in Bash if condition
If you know you're always going to use Bash, it's much easier to always use the double bracket conditional compound command [[ ... ]]
, instead of the POSIX-compatible single bracket version [ ... ]
. Inside a [[ ... ]]
compound, word-splitting and pathname expansion are not applied to words, so you can rely on
if [[ $aug1 == "and" ]];
to compare the value of $aug1
with the string and
.
If you use [ ... ]
, you always need to remember to double quote variables like this:
if [ "$aug1" = "and" ];
If you don't quote the variable expansion and the variable is undefined or empty, it vanishes from the scene of the crime, leaving only
if [ = "and" ];
which is not a valid syntax. (It would also fail with a different error message if $aug1
included white space or shell metacharacters.)
The modern [[
operator has lots of other nice features, including regular expression matching.
jQuery: Return javascript array from Ajax-callback
You should return just the object, not a string of JavaScript which assigns the object to a name. That's NOT a JSON response.
Your code which uses the object should be in the success handler of the ajax call, not separate code somewhere else.
Related Topics
Collision Detection Leading to Color Detection
Firebase Cloud Firestore - Initializing a Collection
How to Access Camera with Swiping Gesture
What Happens While Wrapping and Unwrapping an Optional in Swift
Swift Calendar Class Returning Wrong Date for Weekday
Uinavigationbar Turns White and Won't Close When Using Navigationitem.Searchcontroller in iOS 13
Bold Words That Start with Letter
Swift Guard Else Called on Dictionary Key with Null Value
Struct Hash for Different Types
Swift Full Text Search in Tableview
How to Set Local Notifications Between 8Am and 8Pm Every Day
Navigation Bar Title View Missing in iOS11
How to Highlight a Custom UIbutton That Has No Text or Background