Convert NSInteger to NSUInteger?

前端 未结 4 873
天涯浪人
天涯浪人 2021-02-04 00:30

I am trying to convert a NSInteger to a NSUInteger and I googled it and found no real answer. How would I do this?

4条回答
  •  旧时难觅i
    2021-02-04 01:22

    If you are certain that your NSInteger is greater than or equal to zero, you simply cast it as NSUInteger. You should not be casting to NSUInteger if your NSInteger represents a negative number, as it may lead to inconsistency.

    NSInteger signedInteger = -30;
    NSUInteger unsignedInteger = (NSUInteger)signedInteger;
    

    results in

    integer results

    if (unsignedInteger == signedInteger) {
        // is TRUE
    }
    if (signedInteger == 4294967266) {
        // is FALSE
    }
    if (signedInteger == -30) {
        // is TRUE
    }
    if (unsignedInteger == 4294967266) {
        // is TRUE
    }
    if (unsignedInteger == -30) {
        // is TRUE
    }
    

提交回复
热议问题