您的位置:首页 > 移动开发 > Swift

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

2017-12-11 14:50 141 查看
1. 计算字符串放进label中的高度

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

 let
dic = NSDictionary(object:UIFont.systemFont(ofSize:14),
forKey: NSFontAttributeNameasNSCopying)

 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.一些系统通知

打断时通知

 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)

APP活跃状态的通知
       NotificationCenter.default.addObserver(self,
selector: #selector(onAppDidBecomeActive),
name: NSNotification.Name.UIApplicationDidBecomeActive,
object: nil)

屏幕发生旋转的通知
       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.字符串[b]format拼接[/b]

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

extensionString
{
   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 in0
..< digestLen {
            hash.appendFormat("%02x",
result[i])
        }
        result.deinitialize()
       
       returnString(format:
hash asString)
    }
}

在桥接文件中引头文件
#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: