Skip to content

Instantly share code, notes, and snippets.

@huangboju
Last active December 25, 2021 01:40
Show Gist options
  • Save huangboju/50e241b3c177518991e6dcf70429f342 to your computer and use it in GitHub Desktop.
Save huangboju/50e241b3c177518991e6dcf70429f342 to your computer and use it in GitHub Desktop.

一维数组转二维数组

let count = 3

for (i, answer) in answers.enumerated() {
  if _answers.isEmpty || i % count == 0 {
      _answers.append([answer])
  } else {
      _answers[i / count].append(answer)
  }
}

版本比较

// 6.5.1 < 7.0
let flag = "6.5.1".compare("7.0", options: .numeric) == .orderedAscending
/// true

// 6.5.1 > 7.0
let flag = "6.5.1".compare("7.0", options: .numeric) == .orderedDescending
//  false

进位除法

(a + b - 1) / b

16进制字符串对100求余

func value(from str: String) -> Int {
    var result = 0

    let zeroAscii = Character("0").asciiValue ?? 0
    let aAscii = Character("a").asciiValue ?? 0

    for char in str {
        result = result * 16 % 100
        if "0"..."9" ~= char {
            result += Int(char.asciiValue.flatMap { $0 - zeroAscii } ?? 0)
        } else if "a"..."f" ~= char {
            result += Int(char.asciiValue.flatMap { $0 - aAscii + 10 } ?? 0)
        }
    }
    return result
}

func hexMod(with str: String) -> Int {
    let target = [
        76,
        16,
        56,
        96,
        36
    ]

    var result = 0
    for (i, char) in str.reversed().enumerated() {
        let number = Int("\(char)", radix: 16)!
        if i == 0 {
            result = number
            continue
        }
        let idx = i % 5
        let n = target[idx]
        result += (number * n % 100)
    }
    result %= 100
    return result
}
- (int)calculateHexMod:(NSString *)hexString {
    static NSArray <NSNumber *>*targets;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        targets = @[
                    @76,
                    @16,
                    @56,
                    @96,
                    @36
                    ];
    });

    int result = 0;
    NSInteger strLength = hexString.length;
    for (int i = 0; i < strLength; i++) {
        NSString *subStr = [hexString substringWithRange:NSMakeRange(strLength - 1 - i, 1)];
        int number = (int)strtoull([subStr UTF8String], NULL, 16);
        if (i == 0) {
            result = number;
            continue;
        }
        int idx = i % 5;
        int n = targets[idx].intValue;
        result += (number * n);
    }

    result %= 100;

    return result;
}

打印内存地址

let a = [1, 2, 3, 4]
var b = [1, 2, 3, 4]
print(address(o: a))
a.withUnsafeBufferPointer {
   print($0)
}
b.withUnsafeBufferPointer {
  print($0)
}
b.append(5)
b.withUnsafeBufferPointer {
  print($0)
}

func address(o: UnsafeRawPointer) -> String {
     return String(format: "%018p", Int(bitPattern: o))
}

button图片在右

_bindStatusButton.transform = CGAffineTransformMakeScale(-1.0, 1.0);
_bindStatusButton.titleLabel.transform = CGAffineTransformMakeScale(-1.0, 1.0);
_bindStatusButton.imageView.transform = CGAffineTransformMakeScale(-1.0, 1.0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment