Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我面临的问题是,我无法将字符串或 int 转换为 UInt16。这是我当前的代码:(UInt16)[[defaults stringForKey:@"port"] intValue] [[defaults stringForKey:@"port"] intValue]is8080和(UInt16)[[defaults stringForKey:@"port"] intValue]become51075哪个是错误的。有谁知道这样做吗?
(UInt16)[[defaults stringForKey:@"port"] intValue]
[[defaults stringForKey:@"port"] intValue]
8080
51075
谢谢你的帮助!
我自己找到了答案:
NSNumberFormatter* formatter = [[NSNumberFormatter alloc] init]; NSString *port = [defaults stringForKey:@"port"]; UInt16 portNumber = [[formatter numberFromString:port] unsignedShortValue];