【问题标题】:Changing Parameter Type for Delegate Methods更改委托方法的参数类型
【发布时间】:2013-03-04 19:49:46
【问题描述】:

我有一个以 NSInteger 作为参数的 NSURL 委托方法

- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite 

NSInteger 是一个有符号长整数,其范围为 -2147483647 到 2147483647。我的问题是,有时我得到的值超出了正数范围,这会导致该值变为我不想要的负数。

所以我把 NSInteger 改成了 NSUInteger

 - (void)connection:(NSURLConnection *)connection didSendBodyData:(NSUInteger)bytesWritten totalBytesWritten:(NSUInteger)totalBytesWritten totalBytesExpectedToWrite:(NSUInteger)totalBytesExpectedToWrite 

我的问题是是否可以进行这样的更改?到目前为止,我还没有遇到任何问题,正在调用该方法并且我得到了预期的结果。

我担心可能会发生一些无法预料的问题。

【问题讨论】:

  • 您如何获得超出范围的值?你真的在一个写入块中发送了超过 2GB 的数据?
  • totalBytesExpectedToWrite 有时会超过 2 GB
  • 抱歉,我滚动的不够多。我只看了bytesWritten参数。

标签: ios objective-c delegates nsinteger nsuinteger


【解决方案1】:

这样做很好。将NSInteger 更改为NSUInteger 只是执行普通的转换。但是,我会将NSInteger 保留为方法参数,然后在方法实现开始时将其显式转换为NSUInteger

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-02
    • 2011-03-03
    • 1970-01-01
    相关资源
    最近更新 更多