zoukankan      html  css  js  c++  java
  • iOS开发中常见的一些异常

    iOS开发中常见的异常包括以下几种
    NSInvalidArgumentException
    NSRangeException
    NSGenericException
    NSInternallnconsistencyException
    NSFileHandleOperationException


    NSInvalidArgumentException
    非法参数异常是objective-C代码最常出现的错误,所以平时写代码的时候,需要多加注意,加强对参数的检查,避免传入非法参数导致异常,其中尤以nil参数为甚。

    1、集合数据的参数传递
    比如NSMutableArray,NSMutableDictionary的数据操作

    (1)NSDictionary不能删除nil的key
    NSInvalidArgumentException reason, -[__NSCFDictionary removeObjectForKey:]:attempt to remove nil key

    (2)NSDictionary不能添加nil的对象
    NSInvalidArgumentException reason,***-[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[4]

    (3)不能插入nil的对象
    NSInvalidArgumentException reason,***-[__NSArrayM insertObject:atIndex:]: object cannot be nil

    (4)其他一些nil参数
    NSInvalidArgumentException reason, -[__NSCFString hasSuffix:]: nil argument

    2、其他一些API的使用
    APP一般都会有网络操作,免不了使用网络相关接口,比如NSURL的初始化,不能传入nil的http地址:NSInvalidArgumentException reason,***-[NSURL initFileURLWithPath:]: nil string parameter

    3、为实现的方法
    (1).h文件里函数名,却忘了修改.m文件里对应的函数名
    (2)使用第三方库时,没有添加”-ObjC”flag
    (3)MRC时,大部分情况下是因为对象被提前release了,在我们心里不希望他release的情况下,指针还在,对象已经不在了。
    NSInvalidArgumentException reason =-[UIKBBlurredKeyView candidateList]: unrecognized selector sent to …
    NSInvalidArgumentException reason =-[UIThreadSafeNode_responderForEditing]: unrecognized selector sent …

    NSRangeException
    越界异常(NSRangeException)也是比较常出现的异常,有如下几种类型:
    1、数组最大下标处理错误
    比如数组长度count,index的下标范围[0,count-1],在开发时,可能index的最大值超过数组的范文;
    NSRangeException reason = ***-[_NSarrayM objectAtIndex:]: index 19 beyond bounds [0..15]

    2、下标的值是其他变量赋值
    这样会有很大的不确定性,可能是一个很大的整数值
    NSRangeException reason = ***-[_NSarrayM objectAtIndex:]: index 2147483647 beyond bounds [0..4]
    NSRangeException reason = ***-[_NSarrayM objectAtIndex:]: index 18446744073709551615 beyond bounds [0..4]

    3、使用空数组
    如果一个数组刚刚初始化还是空的,就对它进行相关操作
    NSRangeException reason = ***-[_NSarrayM objectAtIndex:]: index 0 beyond bounds for empty array
    所以,为了避免NSRangeException的发生,必须对传入的index参数进行合法性检查,是否在集合数据的个数范围内。

    NSGenericException
    NSGenericException这个异常最容易出现在foreach操作中,在for in循环中如果修改所遍历的数组,无论你是add或remove,都会出错,比如:

        for (id elem in arr) {

            [arr removeObject:elem];

        }



    执行上面的代码会出现以下错误:
    NSGenericException reason = *** Collection<__NSArrayM:0x175058330>was mutated whild being enumerated.
    原因就在这 “for in”,它的内部遍历使用了类似 Iterator进行迭代遍历,一旦元素变动,之前的元素全部被失效,所以在foreach的循环当中,最好不要去进行元素的修改动作,若需要修改,循环改为for遍历,由于内部机制不同,不会产生修改后结果失效的问题。

    for (NSINteger i=0; i<[arr count]; i++) {

            id elem = [arr objectAtIndex:i];

            [arr removeObject:elem];

        }



    NSMallocException
    这也是内存不足的问题,无法分配足够的内存空间
    NSMallocException reason = ***-[NSConcreteMutableData appendBytes:length:]: unable to allocate memory for length(81310)

    NSFileHandleOperationException
    处理文件时的一些异常,最常见的还是存储空间不足的问题,比如应用频繁的保存文档,缓存资料或者处理比较大的数据:
    NSFileHandleOperationException reason:***-[NSConcreteFileHandle writeData:]: No space left on device
    所以在文件处理里,需要考虑到手机存储空间的问题。


    原文:://blog.csdn.net/qq_16270605/article/details/52259638

  • 相关阅读:
    【LeetCode OJ】Remove Element
    【LeetCode OJ】Remove Duplicates from Sorted Array
    【LeetCode OJ】Swap Nodes in Pairs
    【LeetCode OJ】Merge Two Sorted Lists
    【LeetCode OJ】Remove Nth Node From End of List
    【LeetCode OJ】Two Sum
    【LeetCode OJ】Majority Element
    最长公共子序列问题
    php fopen与file_get_contents的区别
    PHP 技巧集合
  • 原文地址:https://www.cnblogs.com/huangzs/p/10283387.html
Copyright © 2011-2022 走看看