How can I create a CGSize in Swift? This is what I have tried so far (but doesn't work):
var s:CGSize = {10,20}
var s:CGSize = CGMakeSize(10,20)
Your first attempt won't work because C structs don't exist in Swift. You need:
let size = CGSize(width: 20, height: 30)
Or (before Swift 3 only, and even then, not preferred):
let size = CGSizeMake(20,30)
(Not MakeSize).
As of Swift 3 you can no longer use CGSizeMake
The solution for Swift 3 is var size = CGSize(width: 20, height: 30)
Related
The scene size values, set via the Xcode GUI, seem to only apply to iOS and are ignored by macOS.
So I programmatically set the window size of my macOS app, within NSViewController, using:
self.view.frame.size = NSSize(width: 375, height: 650)
or even something like:
let screenWidth = NSScreen.main?.frame.width
let screenHeight = NSScreen.main?.frame.height
let windowSize = NSSize(width: screenWidth!/4, height: screenHeight!/2)
self.view.frame.size = windowSize
Is this the correct way to set the window size or am I missing something?
I am new to Swift for mac and currently I have an NSMenu, which autoresizes in width, when the menu items are long. However, I would like to set a max width, either for NSMenu or NSMenuItem. The NSMenu is created in created programmatically.
Currently, I have tried the setFrameSize or setBoundsSize, but they do nothing or trying setting an NSView with a new rect, but erases all the options that existed on the menu
var menuItems = myMenu.items
menuItems.forEach { item in
let title = item.title
//let autoresizingMask: NSView.AutoresizingMask = [.minXMargin, .minYMargin, .maxXMargin, .maxYMargin]
//item.view?.setBoundsSize(NSSize(width: 100, height: 32)) //= NSMakeRect(0, 0, 100, 32)
item.view?.setFrameSize(NSSize(width: 100, height: 32))
item.view?.needsDisplay = true
or
let v = NSView(frame: NSMakeRect(0, 0, 100, 32))
var menuItems = myMenu.items
menuItems.forEach { item in
item.view = v
}
I am not sure if changing the view is the right thing to do or if I am overwriting the existing view that way. Is there a more straight way to set max width for NSMenu?
EDIT:
The text is already automatically truncated, but still exceeds the preferable max Width limit.
I'm study the swift by the Apple's official book "The Swift Programming Language" and faced a compilation error. I create a structure which has two properties with default values. And attempt to initialise with only one parameter results in a compilation error. What a reason of it?
struct Size {
var width = 0.0, height = 0.0
}
let twoByTwo = Size(width: 2.0, height: 2.0)
let zeroByTwo = Size(height: 2.0)
let zeroByZero = Size()
In Swift whenever we are going to create a structure. Swift automatically creates the default initializer for the particular structure. In your case, you will get a compile-time error in let zeroByTwo = Size(height: 2.0).
Because you are not passing all the parameters required by the default initializer. to fix that issue you can create your own init() functions in your structure as shown below.
struct Size {
var width:Double, height:Double
init(width:Double = 0.0, height:Double = 0.0){
self.width = width
self.height = height
}
}
let twoByTwo = Size(width: 2.0, height: 2.0)
let zeroByTwo = Size(width: 2.0)
let zeroByZero = Size()
let a: UIView = {
let a = UIView()
a.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
return a
}()
I saw a lot of people's Swift source code defining let as this way. I just curious what is the benefit of this way?
In this case, there is no benefit, but if the variable in question were a value type then the benefit would be that you could perform some mutating setup code and still get a constant out of it.
It also lets you hide temporary variables that were only needed to initialize the constant, since they'll only exist inside the closure's scope.
After converting from Swift 2 to Swift 3 (even after converting edit-> convert -> to current swift syntax) I am getting lots of errors. Especially:
I am shown total 90 errors for my project which was working fine in Swift 2 before i downloaded this beta Xcode 8.0 and converted to Swift 3
Is this a conversion mistake I am making?
Most of them are easy fixes, simply by tapping the red button, and having Xcode fix it for you! Others include:
CGRect
Swift 2:
let frame = CGRectMake(0, 0, 20, 20)
Swift 3:
let frame = CGRect(x: 0, y: 0, width: 20, height: 20)
CGPoint
Swift 2:
let point = CGPointMake(0, 0)
Swift 3:
let point = CGPoint(x: 0, y: 0)
CGSize
Swift 2:
let size = CGSizeMake(20, 20)
Swift 3:
let size = CGSize(width: 20, height: 20)
CGRectGetMidX
Swift 2:
CGRectGetMidX(view)
Swift 3:
view.midX
CGRectGetMidY
Swift 2:
CGRectGetMidY(view)
Swift 3:
view.midY
UIColor
Swift 2:
let color = UIColor.redColor()
Swift 3:
let color = UIColor.red
"NS"
Swift 2:
NSTimer
NSData
NSError
Swift 3:
Timer
Data
Error
UserDefaults
Swift 2:
NSUserDefaults.standardUserDefaults().//something
Swift 3:
UserDefaults.standard.//something
And always remember to use the helpful "Fix all in Scope" function which can be found at Editor -> Fix all in Scope
I was converting a project and Xcode was not helping me with any fixes so I resorted to a couple of regex search-and-replaces:-
CGPointMake\((.*),[ ]*([^\)]+)\)
CGPoint(x:$1, y:$2)
CGSizeMake\((.*),[ ]*([^\)]+)\)
CGSize(width:$1, height:$2)
Note they are not aware of nested parentheses , but probably good enough for 90% of cases.