美文网首页
数据类型相互转换

数据类型相互转换

作者: 天地一紫罗 | 来源:发表于2017-03-14 12:34 被阅读0次

    一:NSString转为基本数据类型

    int a = [str intValue];

    float a = [str floatValue];

    BOOL a = [str boolValue];

    二:基本数据类型转为NSString

    NSNumber转NSString:

    NSNumberFormatter* numberFormatter = [[NSNumberFormatteralloc] init];

    B = [numberFormatter stringFromNumber:A];

    nsstring和float 还有int之间的转换

    NSString *tempA = @"123";

    NSString *tempB = @"456";

    1,字符串拼接

    NSString *newString = [NSString stringWithFormat:@"%@%@",tempA,tempB];

    2,int转字符

    NSString *stringInt = [NSString stringWithFormat:@"%d",intString];

    3,float转字符

    NSString *stringFloat = [NSString stringWithFormat:@"%f"

    三:结构体转字符串

    NSStringFromCGPoint(CGPoint point);

    NSStringFromCGVector(CGVector vector);

    NSStringFromCGSize(CGSize size);

    NSStringFromCGRect(CGRect rect);

    NSStringFromCGAffineTransform(CGAffineTransform transform);

    NSStringFromUIEdgeInsets(UIEdgeInsets insets);

    NSStringFromUIOffset(UIOffset offset);

    四:字符串转结构体

    CGPointFromString(NSString *string);

    CGVectorFromString(NSString *string);

    CGSizeFromString(NSString *string);

    CGRectFromString(NSString *string);

    CGAffineTransformFromString(NSString *string);

    UIEdgeInsetsFromString(NSString *string);

    UIOffsetFromString(NSString *string);

    五:基本数据类型转NSNumber

    + (NSNumber *)numberWithChar:(char)value;

    + (NSNumber *)numberWithUnsignedChar:(unsigned char)value;

    + (NSNumber *)numberWithShort:(short)value;

    + (NSNumber *)numberWithUnsignedShort:(unsigned short)value;

    + (NSNumber *)numberWithInt:(int)value;

    + (NSNumber *)numberWithUnsignedInt:(unsigned int)value;

    + (NSNumber *)numberWithLong:(long)value;

    + (NSNumber *)numberWithUnsignedLong:(unsigned long)value;

    + (NSNumber *)numberWithLongLong:(long long)value;

    + (NSNumber *)numberWithUnsignedLongLong:(unsigned long long)value;

    + (NSNumber *)numberWithFloat:(float)value;

    + (NSNumber *)numberWithDouble:(double)value;

    + (NSNumber *)numberWithBool:(BOOL)value;

    + (NSNumber *)numberWithInteger:(NSInteger)value NS_AVAILABLE(10_5, 2_0);

    + (NSNumber *)numberWithUnsignedInteger:(NSUInteger)value NS_AVAILABLE(10_5, 2_0);

    六:NSNumber转基本数据类型

    @property (readonly) char charValue;

    @property (readonly) unsigned char unsignedCharValue;

    @property (readonly) short shortValue;

    @property (readonly) unsigned short unsignedShortValue;

    @property (readonly) int intValue;

    @property (readonly) unsigned int unsignedIntValue;

    @property (readonly) long longValue;

    @property (readonly) unsigned long unsignedLongValue;

    @property (readonly) long long longLongValue;

    @property (readonly) unsigned long long unsignedLongLongValue;

    @property (readonly) float floatValue;

    @property (readonly) double doubleValue;

    @property (readonly) BOOL boolValue;

    @property (readonly) NSInteger integerValue NS_AVAILABLE(10_5, 2_0);

    @property (readonly) NSUInteger unsignedIntegerValue NS_AVAILABLE(10_5, 2_0);

    @property (readonly, copy) NSString *stringValue;

    相关文章

      网友评论

          本文标题:数据类型相互转换

          本文链接:https://www.haomeiwen.com/subject/kdnfnttx.html