이 질문을하기 전에 Stackoverflow의 관련 질문을 검색 한 후 유사한 질문을 발견했습니다 : How to convert Any to Int in Swift. tResult?["result"]
가 String
클래스 인 경우Any를 Int로 변환하는 방법?
let tResult = result as? [String:AnyObject]
let stateCode = tResult?["result"] as? Int
내 필요가있다, 나는 그것이 오히려 nil
보다는, 너무 Int
로 변환 할 것을 권장합니다
내 요구 사항은 적은 것이 아니다. objective-c
에서
, 내가 얻을 수있는 class method
을 쓴 Int
변환 :
+ (NSInteger)getIntegerFromIdValue:(id)value
{
NSString *strValue;
NSInteger ret = 0;
if(value != nil){
strValue = [NSString stringWithFormat:@"%@", value];
if(![strValue isEqualToString:@""] && ![strValue isEqualToString:@"null"]){
ret = [strValue intValue];
}
}
return ret;
}
이 Swift3를 사용하여 유사한 class method
을 쓸 수 있습니까? 이 같은
컴파일러가 해당 라인을 빨리 컴파일할지 모르겠다. –
@ LucaD'Alberti 실제로, 나는 성능을 테스트하지 않았다. 나는 컴파일러가 선을 충분히 빨리 컴파일 할만큼 충분히 똑똑하기를 바란다. :) –