Difference between NSRange and NSMakeRange

▼魔方 西西 提交于 2019-11-28 06:57:05

问题


Is there any difference between:

NSRange(location: 0, length: 5)

and:

NSMakeRange(0, 5)

Because Swiftlint throws a warning when I use NSMakeRange, but I don't know why.

Thanks for the Help :-)


回答1:


The only difference between them is that

NSRange(location: 0, length: 5)

is an initializer for NSRange while

NSMakeRange(0, 5)

is a function which creates a new NSRange instance (by using the same initializer inside most likely) and actually is redundant in Swift. Swift has simply inherited it from Objective-C. I would stick to the former




回答2:


Main difference is that

NSRange(location: 0, length: 24) 

is auto-generated struct init method in Swift and

NSMakeRange(0, 24) 

is just a predefined macro that setts location and length

NS_INLINE NSRange NSMakeRange(NSUInteger loc, NSUInteger len) {
    NSRange r;
    r.location = loc;
    r.length = len;
    return r;
}

In general, result is the same, but if you're Swift use first one and if you're writing ObjC code use second ;)



来源:https://stackoverflow.com/questions/44522626/difference-between-nsrange-and-nsmakerange

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!