在arm64上使用NSScanner处理CGFloat

在arm64上使用NSScanner处理CGFloat

本文介绍了在arm64上使用NSScanner处理CGFloat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然,



<$ p $ p> #if defined(__ LP64__)&&& __LP64__
#define CGFLOAT_TYPE double
#define CGFLOAT_IS_DOUBLE 1
#define CGFLOAT_MIN DBL_MIN
#define CGFLOAT_MAX DBL_MAX
#else
#define CGFLOAT_TYPE float
#define CGFLOAT_IS_DOUBLE 0
#define CGFLOAT_MIN FLT_MIN
#define CGFLOAT_MAX FLT_MAX
#endif

所以代码

  NSScanner * scanner = [NSScanner scannerWithString:string]; 
CGFloat c [components];
[scanner scanFloat:& c [i]]

位的应用程序,被打破为64位。它可以替换为

  NSScanner * scanner = [NSScanner scannerWithString:string]; 
CGFloat c [components];
#if CGFLOAT_IS_DOUBLE
[scanner ScanDouble:& c [i]];
#else
[scanner scanFloat:& c [i]];
#endif

但是有更好的选择吗?

解决方案

NSScanner 只支持,所以你需要在某处放置#if指令。也就是说,如果你经常使用这个,你可以子类NSScanner或使用,使代码更加优雅。



我认为基于类别的代码看起来像:



NSScanner + MyAdditions.h:

  #importNSScanner.h

@interface NSScanner(MyAdditions)
- (BOOL)my_scanCGFloat:(CGFloat *)cgFloatValue
@end

NSScanner + MyAdditions.m:

  #importNSScanner + MyAdditions .h

@implementation NSScanner(MyAdditions)
- (BOOL)my_scanCGFloat:(CGFloat *)cgFloatValue {
#if CGFLOAT_IS_DOUBLE
return [self scanDouble:cgFloatValue ];
#else
return [self scanFloat:cgFloatValue];
#endif
}
@end


Apparently CGFloat is double on arm64:

#if defined(__LP64__) && __LP64__
# define CGFLOAT_TYPE double
# define CGFLOAT_IS_DOUBLE 1
# define CGFLOAT_MIN DBL_MIN
# define CGFLOAT_MAX DBL_MAX
#else
# define CGFLOAT_TYPE float
# define CGFLOAT_IS_DOUBLE 0
# define CGFLOAT_MIN FLT_MIN
# define CGFLOAT_MAX FLT_MAX
#endif

So the code

NSScanner *scanner = [NSScanner scannerWithString:string];
CGFloat c[components];
[scanner scanFloat:&c[i]]

which was working fine for 32-bit apps, is broken for 64-bit. It can be replaced with

NSScanner *scanner = [NSScanner scannerWithString:string];
CGFloat c[components];
#if CGFLOAT_IS_DOUBLE
   [scanner scanDouble:&c[i]];
#else
   [scanner scanFloat:&c[i]];
#endif

But is there a better alternative?

解决方案

NSScanner only supports a few basic types, so you're going to need to have that #if directive somewhere. That said, if you're using this often you could subclass NSScanner or use a category to make the code more elegant.

I think the category-based code would look something like:

NSScanner+MyAdditions.h:

#import "NSScanner.h"

@interface NSScanner (MyAdditions)
  - (BOOL)my_scanCGFloat:(CGFloat *) cgFloatValue
@end

NSScanner+MyAdditions.m:

#import "NSScanner+MyAdditions.h"

@implementation NSScanner (MyAdditions)
  - (BOOL)my_scanCGFloat:(CGFloat *) cgFloatValue {
#if CGFLOAT_IS_DOUBLE
    return [self scanDouble:cgFloatValue];
#else
    return [self scanFloat:cgFloatValue];
#endif
}
@end

这篇关于在arm64上使用NSScanner处理CGFloat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 19:19