Skip to content

Instantly share code, notes, and snippets.

View josephchang10's full-sized avatar

张嘉夫 josephchang10

View GitHub Profile
import UIKit
func sizeHeightWithText(labelText: String,
fontSize: CGFloat,
textAttributes: [String : AnyObject]) -> CGRect {
return labelText.boundingRect(
with: CGSize(width:fontSize, height:480),
options: NSStringDrawingOptions.usesLineFragmentOrigin,
attributes: textAttributes, context: nil)
@josephchang10
josephchang10 / size.swift
Created January 12, 2017 09:30
根据文字计算高度
let fontSize: CGFloat = 22.0
func sizeHeightWithText(labelText: NSString,
fontSize: CGFloat,
textAttributes: [String : Any]) -> CGRect {
return labelText.boundingRect(
with: CGSize(width:fontSize, height:480),
options: NSStringDrawingOptions.usesLineFragmentOrigin,
attributes: textAttributes, context: nil)
@josephchang10
josephchang10 / generic.swift
Created December 21, 2016 07:49
Swift 泛型
func swapTwoValues<Joseph>(_ a: inout Joseph, _ b: inout Joseph) {
let temporaryA = a
a = b
b = temporaryA
}
var someInt = 3
var anotherInt = 107
swapTwoValues(&someInt, &anotherInt)
print("someInt 现在是\(someInt),anotherInt 现在是 \(anotherInt)")
@josephchang10
josephchang10 / inout.swift
Created December 21, 2016 07:40
输入输出参数
func swapTwoInts(_ a: inout Int, _ b: inout Int) {
let temporaryA = a
a = b
b = temporaryA
}
var someInt = 3
var anotherInt = 107
swapTwoInts(&someInt, &anotherInt)
print("someInt 现在是 \(someInt),anotherInt 现在是 \(anotherInt)")
@josephchang10
josephchang10 / coredata.swift
Created December 21, 2016 03:11
Core Data ManagedObjectContextSettable 协议
protocol ManagedObjectContextSettable: class {
var managedObjectContext: NSManagedObjectContext! { get set }
}
@josephchang10
josephchang10 / optional.swift
Created December 20, 2016 09:35
可选值
let nickName: String? = nil
let fullName: String = "John Appleseed"
let informalGreeting = "Hi \(nickName ?? fullName)"
func objectAtIndexPath(indexPath: NSIndexPath) -> Object {
guard let result = fetchedResultsController.objectAtIndexPath(indexPath)
as? Object else
{
fatalError("Unexpected object at \(indexPath)")
}
return result
}
@josephchang10
josephchang10 / selector.m
Created December 16, 2016 10:08
Selector
-(void) callMe {
//...
}
-(void) callMeWithParam:(id)obj {
//...
}
SEL someMethod = @selector(callMe);
SEL anotherMethod = @selector(callMeWithParam:);
@josephchang10
josephchang10 / Swifter.playground
Last active December 16, 2016 09:59
柯里化
func addTo(_ adder: Int) -> (Int) -> Int {
return {
num in
return num + adder
}
}
let addTwo = addTo(2) //addTwo:Int -> Int
let result = addTwo(6) //result = 8
@josephchang10
josephchang10 / ViewController.swift
Created December 16, 2016 09:11
didReceiveMemoryWarning
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// 在这里销毁所有可以被重建的资源
}