ksnowlv

回顾过去,总结以往;立足现在,铭记当下;技术为主,笔记而已.

takeUnretainedValue与takeRetainedValue区别

| Comments

swift中,如果我们使用perform相关函数时,例如

1
2
3
4
 if let object = connectionItem.perform(connectionSEL, with: nil) {
                    (object as! YKURLConnection).cancel()

                }

此时会报警告

 Cast from 'Unmanaged<AnyObject>' to unrelated type 'YKURLConnection' always fails

虽然代码可以正常运行,报这种警告,也是让人心理很不爽。

Swift-散列算法md5

| Comments

swift中散列算法md5很常用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    func md5() -> NSString {

        guard self.length > 0 else {
            return self
        }

        let utf8Buf = self.cString(using: String.Encoding.utf8.rawValue)
        let length = self.lengthOfBytes(using: String.Encoding.utf8.rawValue)

        let buffer = UnsafeMutablePointer<CUnsignedChar>.allocate(capacity: Int(CC_MD5_DIGEST_LENGTH))
        buffer.initialize(to: 0)
        CC_MD5(utf8Buf,CC_LONG(length), buffer)

        let md5String = NSMutableString()

        for i in 0 ..< CC_MD5_DIGEST_LENGTH {
            md5String.appendFormat("%02x", buffer[Int(i)])
        }

        buffer.deallocate()

        return md5String.uppercased as NSString
    }

记得在bridging-header中导入头文件

  • CommonCrypto/CommonDigest.h

Swift之url编解码

| Comments

url的编码与解码,在工程中很常用,编解码针对特殊字符及中文,确保url可以正常访问

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
let KUrlCodingReservedCharacters = "!*'();:|@&=+$,/?%#[]{}"

extension NSString {

    /// url编码
    ///
    /// - Returns: NSString
    func urlEncode() -> NSString? {
        return self.addingPercentEncoding(withAllowedCharacters: CharacterSet(charactersIn: KYKUrlCodingReservedCharacters).inverted)! as NSString

    }

    /// url解码
    ///
    /// - Returns: NSString
    func urlDecode() -> NSString? {
        return self.removingPercentEncoding as NSString?
    }
}

iOS指针集合类

| Comments

Collections之Pointer Collections :指针集合类

    1. NSPointerArray
    1. NSHashTable
    1. NSMapTable

1.NSPointerFunctionsOptions

Defines the memory and personality options for an NSPointerFunctions object.

  • NSPointerFunctionsMachVirtualMemory
  • NSPointerFunctionsMallocMemory

       Use free() on removal, calloc() on copy in.
    
  • NSPointerFunctionsOpaqueMemory

  • NSPointerFunctionsStrongMemory
  • NSPointerFunctionsWeakMemory

      Uses weak read and write barriers appropriate for ARC or GC. Using NSPointerFunctionsWeakMemory object references will turn to NULL on last release.
    
  • NSMapTableStrongMemory

    Equivalent to NSPointerFunctionsStrongMemory .

  • NSMapTableWeakMemory

      Equivalent to NSPointerFunctionsWeakMemory.
    

2. NSPointerArray/NSHashTable/NSMapTable

  • 可以存储所有指针类型的数据包括nil
  • 可以添加或删除nil values
  • 可以弱引用或强引用对象。
  • 添加元素时,性能很差。慎用!!!
Included file 'custom/after_footer.html' not found in _includes directory