Skip to content

Instantly share code, notes, and snippets.

View ylem's full-sized avatar

WEI LU ylem

  • TaoTe.tech
  • Manchester
View GitHub Profile
@ylem
ylem / sort_dictionary_array.m
Created October 29, 2012 22:11
sort dictionaries by key's value in an array
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!");
@ylem
ylem / timeFormatter.m
Created November 5, 2012 11:27
Time string formatter
- (NSString*)timeString
{
NSDateFormatter *formatter;
NSString *dateString;
formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"dd-MM-yyyy HH:mm:ss"];
dateString = [formatter stringFromDate:[NSDate date]];
return dateString;
@ylem
ylem / ios_singleton.m
Last active November 21, 2015 13:58
Object-C create singleton class
+ (instancetype)sharedInstance
{
static MyClass *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[MyClass alloc] init];
});
return sharedInstance;
}
@ylem
ylem / gist:528c3deb2abcdbf14696
Last active August 29, 2015 14:13
Calculate word count
NSString *string = @"中华人民共和国万岁 万岁 万万岁";
NSCountedSet *countedSet = [NSCountedSet new];
[string enumerateSubstringsInRange:NSMakeRange(0, [string length])
options:NSStringEnumerationByComposedCharacterSequences | NSStringEnumerationLocalized
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop){
_sumText++;
[countedSet addObject:substring];
}];
@ylem
ylem / BezierCurveMove
Last active August 29, 2015 14:24
Move SKSpriteNode by Bezier Curve Path
func BezierCurveMove(screenSize: CGSize, node: SKSpriteNode) {
let size = node.size
var cgpath = CGPathCreateMutable()
//random values
let xStart = Utility.randInRange(Int(size.width)...Int(screenSize.width-size.width))
let xEnd = Utility.randInRange(Int(size.width)...Int(screenSize.width-size.width))
//ControlPoint1
let cp1X = Utility.randInRange(Int(size.width)...Int(screenSize.width-size.width))
# Change the ownership and permissions of "//usr//local" back to your user account!
sudo chown -R $(whoami):admin /usr/local
@ylem
ylem / Utility.m
Last active April 14, 2016 14:58
show 1st, 2nd, 3rd, 4th... day format
#import <Foundation/Foundation.h>
@interface Utlity : NSObject
+ (NSString *)formatDaySuffixForDate:(NSDate *)date;
+ (NSString *)formatDayWithSuffixForDate:(NSDate *)date withWeekday:(BOOL)showWeekday;
@end
@implementation Utlity
@ylem
ylem / RomanArabic.cs
Last active November 18, 2016 09:48
Roman To Arabic
using System;
using NUnit.Framework;
namespace RomanTest
{
public class RomanArabic
{
public RomanArabic() { }
[TestCase("M", 1000)]
@ylem
ylem / uploadImage.m
Created January 26, 2017 18:39
Upload image by NSURLSession
/**
* Generate a mutable URLRequest with authorisation Token (if has) and User-Agent
*
* @param url NSURL object
* @param method GET, POST, PUT or DELETE
* @return NSMutableURLRequest object, can be modify (add more values) later.
*/
- (NSMutableURLRequest *)requestUrl:(NSURL *)url method:(NSString *)method {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
@ylem
ylem / CenterItemInCollectionView.playground
Last active January 5, 2024 03:28
Scrolling item on a horizontal UICollectionView, stopped item on center of screen.
//: Playground - noun: a place where people can play
import UIKit
import PlaygroundSupport
class WLCollectionCell: UICollectionViewCell {
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}