Skip to content

Instantly share code, notes, and snippets.

View josephchang10's full-sized avatar

张嘉夫 josephchang10

View GitHub Profile
@josephchang10
josephchang10 / blockchain.swift
Created December 27, 2017 14:25
在 iOS 中实现区块链
import Cocoa
import Foundation
class Block {
var index: Int = 0
var dateCreated: String
var previousHash: String!
var hash: String!
var nonce: Int
@josephchang10
josephchang10 / ReviewRequest.swift
Created August 12, 2017 05:12
从 iOS 10.3 开始请求应用商店评分的正确姿势:SKStoreReviewController
//
// ReviewRequest.swift
//
// Created by 张嘉夫 on 12/8/17.
// Copyright © 2017 张嘉夫. All rights reserved.
import Foundation
import StoreKit
let runIncrementerSetting = "numberOfRuns" // 用于存储运行次数的 UserDefauls 字典键
@josephchang10
josephchang10 / str.swift
Created November 8, 2017 23:10
The more elegant way to remove all characters after specific character in the String object in Swift
var str = "str.str"
if let dotRange = str.range(of: ".") {
str.removeSubrange(dotRange.lowerBound..<str.endIndex)
}
@josephchang10
josephchang10 / CoreDataStack.swift
Created November 6, 2017 09:16
Core Data Stack
//
// CoreDataStack.swift
// ChineseNewWords
//
// Created by 张嘉夫 on 06/11/2017.
// Copyright © 2017 张嘉夫. All rights reserved.
//
import Foundation
import CoreData
@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];
}
@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 / MainThread.swift
Created April 14, 2017 02:20
Swift 获取主线程
DispatchQueue.main.async {
self.imageView.image = image
}
@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 / 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)];
}