Skip to content

Instantly share code, notes, and snippets.

View josephchang10's full-sized avatar

张嘉夫 josephchang10

View GitHub Profile
@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 / 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 / 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)
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 / FavoriteViewController.m
Created January 22, 2017 01:31
初次加载隐藏搜索框
- (void)initSearchBar
{
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
[searchBar sizeToFit];
searchBar.placeholder = [IFI18nUtils getLocalizedString:@"SEARCH"];
searchBar.delegate = self;
searchBar.backgroundColor = [UIColor clearColor];
[self.collectionView setContentInset:UIEdgeInsetsMake(-44, 0, 0, 0)];
}
@josephchang10
josephchang10 / FavoriteViewController.m
Created January 22, 2017 01:31
初次加载隐藏搜索框
- (void)initSearchBar
{
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
[searchBar sizeToFit];
searchBar.placeholder = [IFI18nUtils getLocalizedString:@"SEARCH"];
searchBar.delegate = self;
searchBar.backgroundColor = [UIColor clearColor];
[self.collectionView setContentInset:UIEdgeInsetsMake(-44, 0, 0, 0)];
}
@josephchang10
josephchang10 / sort.m
Created January 23, 2017 02:03
Best way to sort an NSArray of NSDictionary objects
NSSortDescriptor * descriptor = [[NSSortDescriptor alloc] initWithKey:@"interest" ascending:YES];
stories = [stories sortedArrayUsingDescriptors:@[descriptor]];
recent = [stories copy];
@josephchang10
josephchang10 / MainThread.swift
Created April 14, 2017 02:20
Swift 获取主线程
DispatchQueue.main.async {
self.imageView.image = image
}
@josephchang10
josephchang10 / orientation.swift
Created April 14, 2017 15:29
Setting device orientation in Swift iOS
override var prefersStatusBarHidden: Bool {
return true
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return [UIInterfaceOrientationMask.portrait, UIInterfaceOrientationMask.portraitUpsideDown]
}
@josephchang10
josephchang10 / shuffle.m
Created April 18, 2017 10:25
How to shuffle a NSMutableArray [Objective-C]
/* myArray is a NSMutableArray with some objects */
NSUInteger count = [myArray count];
for (NSUInteger i = 0; i < count; ++i) {
int nElements = count - i;
int n = (arc4random() % nElements) + i;
[myArray exchangeObjectAtIndex:i withObjectAtIndex:n];
}