简体   繁体   中英

In Swift, how do you convert a CGSize into its string representation?

我知道在 Objective-C 中你使用NSStringFromCGSize(size)CGSize转换为它的字符串表示,但它在 Swift 中的等价物是什么?

Simply call NSStringFromCGSize(size)

var size = CGSize(width: 10, height: 10)
var str = NSStringFromCGSize(size)

it's this quite simple in swift ;) The method in swift returns a String

If you use the () syntax to convert, as mentioned below, it will be a different result to the call of the convert method.

var size = CGSize(width: 10, height: 10)
var str = NSStringFromCGSize(size) // "{10, 10}"
var str2 = "size: \(size)" //"size: (10.0, 10.0)"

I tested it out in a playground. And when i look at result i would use the convert method.

These days, you use this:

let size: CGSize
let str = NSCoder.string(for: size)

https://developer.apple.com/documentation/foundation/nscoder/1624514-string

They are the same:

import UIKit

var _size = CGSize.zeroSize
println("Size: \(NSStringFromCGSize(_size))") // "Size: {0, 0}"

Reference: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIKitFunctionReference/#//apple_ref/c/func/NSStringFromCGSize

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM