Skip to content

Instantly share code, notes, and snippets.

张嘉夫 josephchang10

Block or report user

Report or block josephchang10

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View DiaryLabel.swift
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 Jan 12, 2017
根据文字计算高度
View size.swift
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)
View generic.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)")
View inout.swift
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 Dec 21, 2016
Core Data ManagedObjectContextSettable 协议
View coredata.swift
protocol ManagedObjectContextSettable: class {
var managedObjectContext: NSManagedObjectContext! { get set }
}
View optional.swift
let nickName: String? = nil
let fullName: String = "John Appleseed"
let informalGreeting = "Hi \(nickName ?? fullName)"
View guard.swift
func objectAtIndexPath(indexPath: NSIndexPath) -> Object {
guard let result = fetchedResultsController.objectAtIndexPath(indexPath)
as? Object else
{
fatalError("Unexpected object at \(indexPath)")
}
return result
}
View selector.m
-(void) callMe {
//...
}
-(void) callMeWithParam:(id)obj {
//...
}
SEL someMethod = @selector(callMe);
SEL anotherMethod = @selector(callMeWithParam:);
View Swifter.playground
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
View ViewController.swift
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// 在这里销毁所有可以被重建的资源
}
You can’t perform that action at this time.