swift 开发过程中的一些小总结

1. 计算字符串放进label中的高度


  let size = CGSize (width: WIDTH - 20 , height: 0 )

  let dic = NSDictionary (object: UIFont . systemFont (ofSize: 14 ), forKey: NSFontAttributeName as NSCopying )

  let strSize = videoBody. summary ?. boundingRect (with: size, options: . usesLineFragmentOrigin , attributes: dic as ? [ String : Any ], context: nil ). size

self . label . frame = CGRect (x: 0 ,y: 0 ,width: WIDTH-20 ,height: strSize?. height !)

2.一些系统通知

  1. 打断时通知
  NotificationCenter . default . addObserver ( self , selector: #selector ( onAudioSessionEvent ), name: NSNotification . Name . AVAudioSessionInterruption , object: nil )

     2.进入后台的通知

NotificationCenter . default . addObserver ( self , selector: #selector ( onAppDidEnterBackGround ), name: NSNotification . Name . UIApplicationDidEnterBackground , object: nil )

   3.将要进入前台的通知

        NotificationCenter . default . addObserver ( self , selector: #selector ( onAppWillEnterForeground ), name: NSNotification . Name . UIApplicationWillEnterForeground , object: nil )
  1. APP活跃状态的通知
        NotificationCenter . default . addObserver ( self , selector: #selector ( onAppDidBecomeActive ), name: NSNotification . Name . UIApplicationDidBecomeActive , object: nil )
  1. 屏幕发生旋转的通知
        NotificationCenter . default . addObserver ( self , selector: #selector ( statusBarOrientationChanged ), name: NSNotification . Name . UIApplicationDidChangeStatusBarOrientation , object: nil )



3.GCD多线程

      DispatchQueue.global().async {
               //耗时操作


               DispatchQueue.main.async {
                   //通知ui刷新


                }

            }

4.求余运算

let  value1 = 5.5
let  value2 = 2.2
 
let  result = value1.truncatingRemainder(dividingBy: value2)

5.字符串format拼接

url:String(format:"%@/activity/listActivitys?start=%ld&limit=%ld",arguments[BASEADDRESS,withStart,withLimit])


6.判断数组是否有某元素   contains where

    判断self.titleBodyArr数组中是否有id == 11的body

   let has =self.titleBodyArr.contains(where: { (bo:FLGPindaoBody) ->Boolin

       bo.id == 11

    })


    if has ==false{

       self.dataArr.append(body)

    }

7.md5 加密

创建文件


加入代码


import Foundation

extension String {
    var md5 : String {
        let str = self . cString (using: String . Encoding . utf8 )
        let strLen = CC_LONG ( self . lengthOfBytes (using: String . Encoding . utf8 ))
        let digestLen = Int ( CC_MD5_DIGEST_LENGTH )
        let result = UnsafeMutablePointer < CUnsignedChar >. allocate (capacity: digestLen)
       
        CC_MD5 (str!, strLen, result)
       
        let hash = NSMutableString ()
        for i in 0 ..< digestLen {
            hash. appendFormat ( "%02x" , result[i])
        }
        result. deinitialize ()
       
        return String (format: hash as String )
    }
}

在桥接文件中引头文件
#import <CommonCrypto/CommonDigest.h>

调用方法:     let passStr = pwd.md5


8. 图片 大小适应 UIimage

1. UIimageview 按图片最短边 完整填充 不变形 

        bigImageView?.contentScaleFactor =UIScreen.main.scale

        bigImageView?.contentMode =UIViewContentMode.scaleAspectFill

        bigImageView?.autoresizingMask =UIViewAutoresizing.flexibleHeight

        bigImageView?.clipsToBounds =true


2.图片完全显示 不变形

        bigImageView?.contentScaleFactor = UIScreen.main.scale

        bigImageView?.contentMode = UIViewContentMode.scaleAspectFit

        bigImageView ?. clipsToBounds  =  true




猜你喜欢

转载自blog.csdn.net/flg1554112450/article/details/78772328