Objective C 扩展等同于 Swift 扩展?
Objective C Extension equivalent to Swift Extensions?
Objective-C 和 Swift 中的扩展等同于什么?它与在 class 中创建 class 函数一样吗?
extension CGRect{
static func rectWithTwoPoints(p1:CGPoint,p2:CGPoint) -> CGRect
{
return CGRectMake(min(p1.x, p2.x),min(p1.y, p2.y),fabs(p1.x - p2.x),fabs(p1.y - p2.y));
}
}
没有单一的等价物,它们是具有不同功能的不同语言。
对于您的示例,'equivalent' 将是在某处声明的实用函数,可能只是在文件中,因为 CGRect
不是 class。它将是一个 C 函数,而不是一个 Obj-C 方法。
你甚至可以为它声明一个宏。
在 Objective-C 中,结构与 类 不同,而是普通的 C。因此它们没有与之关联的函数。通常的模式是有 (C) 函数处理结构。您会在 header 中找到 a bunch of them,例如:
CGRect CGRectMake ( CGFloat x, CGFloat y, CGFloat width, CGFloat height );
如果你想有一个额外的功能,就这样写:
CGRect rectWithTwoPoints(CGPoint p1, CGPoint p2)
{
return CGRectMake(min(p1.x, p2.x),min(p1.y, p2.y),fabs(p1.x - p2.x),fabs(p1.y - p2.y));
}
并将原型放入header,如果你想在定义编译单元之外使用它:
CGRect rectWithTwoPoints(CGPoint p1, CGPoint p2); // <- There is a semicolon for prototyping
在objective C中是category
,在swift中是extension
1.Click File -> New -> File
2.Select Objective-C
文件在 iOS 或 Mac OS 中的 Sources
下,然后单击下一步
3.Now select 文件类型为 Category
Select UIView
作为 category
的基类并将名称设置为 "UIView+CGRect"
你可以添加你的方法,比如
UIView+CGRect.h 类别:
+ (CGRect) rectWithTwoPoints:(CGPoint) p1 andWith:(CGPoint) p2;
UIView+CGRect.m 类别:
+ (CGRect) rectWithTwoPoints:(CGPoint) p1 andWith:(CGPoint) p2 {
return CGRectMake(MIN(p1.x, p2.x), MIN(p1.y, p2.y), fabs(p1.x - p2.x), fabs(p1.y - p2.y));
}
然后只需将您的类别导入视图控制器中您要使用它的位置并像访问一样访问
在ViewController.h
#import "UIView+CGRect.h"
代码将是
CGrect rect = [UIView rectWithTwoPoints:POINT_ONE andWith:rectWithTwoPoints:POINT_TWO];
你会得到想要的结果。
Objective-C 和 Swift 中的扩展等同于什么?它与在 class 中创建 class 函数一样吗?
extension CGRect{
static func rectWithTwoPoints(p1:CGPoint,p2:CGPoint) -> CGRect
{
return CGRectMake(min(p1.x, p2.x),min(p1.y, p2.y),fabs(p1.x - p2.x),fabs(p1.y - p2.y));
}
}
没有单一的等价物,它们是具有不同功能的不同语言。
对于您的示例,'equivalent' 将是在某处声明的实用函数,可能只是在文件中,因为 CGRect
不是 class。它将是一个 C 函数,而不是一个 Obj-C 方法。
你甚至可以为它声明一个宏。
在 Objective-C 中,结构与 类 不同,而是普通的 C。因此它们没有与之关联的函数。通常的模式是有 (C) 函数处理结构。您会在 header 中找到 a bunch of them,例如:
CGRect CGRectMake ( CGFloat x, CGFloat y, CGFloat width, CGFloat height );
如果你想有一个额外的功能,就这样写:
CGRect rectWithTwoPoints(CGPoint p1, CGPoint p2)
{
return CGRectMake(min(p1.x, p2.x),min(p1.y, p2.y),fabs(p1.x - p2.x),fabs(p1.y - p2.y));
}
并将原型放入header,如果你想在定义编译单元之外使用它:
CGRect rectWithTwoPoints(CGPoint p1, CGPoint p2); // <- There is a semicolon for prototyping
在objective C中是category
,在swift中是extension
1.Click File -> New -> File
2.Select Objective-C
文件在 iOS 或 Mac OS 中的 Sources
下,然后单击下一步
3.Now select 文件类型为 Category
Select UIView
作为 category
的基类并将名称设置为 "UIView+CGRect"
你可以添加你的方法,比如
UIView+CGRect.h 类别:
+ (CGRect) rectWithTwoPoints:(CGPoint) p1 andWith:(CGPoint) p2;
UIView+CGRect.m 类别:
+ (CGRect) rectWithTwoPoints:(CGPoint) p1 andWith:(CGPoint) p2 {
return CGRectMake(MIN(p1.x, p2.x), MIN(p1.y, p2.y), fabs(p1.x - p2.x), fabs(p1.y - p2.y));
}
然后只需将您的类别导入视图控制器中您要使用它的位置并像访问一样访问
在ViewController.h
#import "UIView+CGRect.h"
代码将是
CGrect rect = [UIView rectWithTwoPoints:POINT_ONE andWith:rectWithTwoPoints:POINT_TWO];
你会得到想要的结果。