学习swift遇到的问题

1.遍历
在 for-in 遍历存放字典的数组时,出现如下错误,

学习swift遇到的问题_第1张图片
错误信息
编译错误

编译失败的原因是 dictionary不知道是哪种类型的,需强制转换成NSDictionary类型。


学习swift遇到的问题_第2张图片
错误信息

编译成功

2.定义全局变量
var obj = class() //初始化obj,相当于class.init(),分配内存
var obj : class? =nil //初始化obj,为空。
3.对象类型的判断
在oc中,可以使用-isKindOfClass方法判断。
在swift中,用 -is 判断:if obj is class {}
4.重写init方法是,必须增加下面的方法

requiredinit?(coder aDecoder:NSCoder) {
super.init(coder: aDecoder)
fatalError("init(coder:) has not been implemented")
}

5.C语言指针导入到Swift中
1)UnsafePointerUnsafeMutablePointer
声明为常量的指针被导入为UnsafePointer,非常量的指针则被导入为UnsafeMutablePoinger。
比如:

C:
void myFunction(const int *myConstIntPointer);
Swift:
func myFunction(myConstIntPointer: UnsafePointer)
C:
void iTakeAVoidPointer(void *aVoidPointer);
Swift:
func iTakeAVoidPointer(aVoidPointer: UnsafeMutablePointer)

更多详见:http://www.cocoachina.com/swift/20141014/9900.html
6.String 转float、int

funcStringToFloat(str:String)->(CGFloat)
{
  letstring = str
  varcgFloat:CGFloat=0
  ifletdoubleValue =Double(string)
  {
    cgFloat =CGFloat(doubleValue)
  }
    returncgFloat
}
funcstringToInt(str:String)->(Int)
{
  letstring = str
  varint:Int?
  ifletdoubleValue =Int(string) 
  {
    int =Int(doubleValue)
  }
    ifint ==nil
  {
    return0
  }
    returnint!
}

7. 添加通知(NotificationCenter)

let notification  = NotificationCenter.default
let name = NSNotification.Name.init("")
notification.addObserver(self, selector: #selector(), name:name, object: nil)

8. for 循环倒叙

for i in (0...10).reversed() {
    print(i)
}

输出就是 10,9,8...........
9 oc block 与 swift 闭包
http://www.open-open.com/lib/view/open1474524707276.html
10 拍照保存到本地

//选择照片、拍照后回调
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
    {
        picker.dismiss(animated: true, completion: nil)
        if (picker.sourceType == .camera)    //拍照模式下
        {
            UIImageWriteToSavedPhotosAlbum(info["UIImagePickerControllerOriginalImage"] as! UIImage, self, #selector(DrSelectedDrugInformationViewController.image(image:didFinishSavingWithError:contextInfo:)), nil)
        }
    }
 //保存图片
    func image(image: UIImage, didFinishSavingWithError: NSError?, contextInfo: AnyObject)
    {
        if didFinishSavingWithError != nil {
            self.showHint("保存失败")
            return
        }
    }

特别注意的是,在

 UIImageWriteToSavedPhotosAlbum(info["UIImagePickerControllerOriginalImage"] as! UIImage, self, #selector(DrSelectedDrugInformationViewController.image(image:didFinishSavingWithError:contextInfo:)), nil)

,#selector()语句中,要调用类名.方法名,否则会cash
10 重写侧滑

 //重写侧滑
    func handleNavigationTransition()
    {
        let target = self.navigationController?.interactivePopGestureRecognizer!.delegate
        let pan = UIPanGestureRecognizer(target:target,
                                         action:Selector(("handleNavigationTransition:")))
        pan.delegate = self
        self.view.addGestureRecognizer(pan)
        //同时禁用系统原先的侧滑返回功能
        self.navigationController?.interactivePopGestureRecognizer!.isEnabled = false
    }
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool
    {
        if self.childViewControllers.count == 1     //没有push的界面不会触发侧滑
        {
            return false
        }
        return true
    }

需要UIGestureRecognizerDelegate

你可能感兴趣的:(学习swift遇到的问题)