小编ver*_*rec的帖子

如何在switch语句之外访问Swift枚举关联值

考虑:

enum Line {
    case    Horizontal(CGFloat)
    case    Vertical(CGFloat)
}

let leftEdge             =  Line.Horizontal(0.0)
let leftMaskRightEdge    =  Line.Horizontal(0.05)
Run Code Online (Sandbox Code Playgroud)

如何在lefEdge不使用switch语句的情况下直接访问相关值?

let noIdeaHowTo          = leftEdge.associatedValue + 0.5
Run Code Online (Sandbox Code Playgroud)

这甚至没有编译!

我看了一下这些 SO 问题,但没有一个答案似乎解决这个问题.

noIdeaHowTo上面的非编译行应该是单行,但由于associated value可以是任何类型,我甚至无法看到用户代码如何在le enum本身中编写"泛型"get或associatedValue方法.

我最终得到了这个,但它很严重,每次我添加/修改案例时都需要我重新访问代码...

enum Line {
    case    Horizontal(CGFloat)
    case    Vertical(CGFloat)

    var associatedValue: CGFloat {
        get {
            switch self {
                case    .Horizontal(let value): return value
                case    .Vertical(let value): return value
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

有人指点吗?

enums swift

48
推荐指数
5
解决办法
2万
查看次数

递归阻塞并保留ARC中的循环

EDIT2:

不.建议的答案是关于异步调用.我想要并需要同步调用,就像在普通的标准递归调用中一样.

编辑:

__unsafe_unretained void (^unsafe_apply)(UIView *, NSInteger) ;
Run Code Online (Sandbox Code Playgroud)

在没有警告或错误的情况下编译,它在运行时失败,并将NULL存储到unsafe_apply中.

不过这个:

- (void) applyToView: (UIView *) view {

    UIColor * (^colorForIndex)(NSInteger) = ^(NSInteger index) {
        return [UIColor colorWithHue: ((CGFloat) index / 255.0f)
                          saturation: 0.5f
                          brightness: 0.5f
                               alpha: 1.0f] ;
    } ;

    void (^applyColors) (UIView *, NSInteger index) = ^(UIView * view, NSInteger index) {
        view.backgroundColor = colorForIndex(index) ;
    } ;

    void (^__block recurse_apply)(UIView *, NSInteger) ;

    void (^apply)(UIView *, NSInteger) = ^(UIView * view, NSInteger level) { …
Run Code Online (Sandbox Code Playgroud)

recursion objective-c objective-c-blocks automatic-ref-counting retain-cycle

20
推荐指数
1
解决办法
7270
查看次数

旋转UIView坐标后,但UIWindow不是?

使用Xcode 4.2.1 iPad iOS 5.0.1,创建一个新的"单视图"iPad项目.在控制器中,添加:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

- (void) dumpView: (UIView *) view {
    CGRect frame = view.frame ;
    CGRect bounds = view.bounds ;
    CGPoint center = view.center ;

    NSLog(@"view [%@]:%d frame=%@ bounds=%@ center=%@"
          ,   NSStringFromClass(view.class)
          ,   [view hash]
          ,   NSStringFromCGRect(frame)
          ,   NSStringFromCGRect(bounds)
          ,   NSStringFromCGPoint(center)) ;
}

- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation) fromInterfaceOrientation {

    NSLog(@"INViewController.didRotateFromInterfaceOrientation: %d to %d", fromInterfaceOrientation, self.interfaceOrientation) ;
    [self dumpView: self.view] ;
    [self dumpView: self.view.superview] ;
}
Run Code Online (Sandbox Code Playgroud)

运行它,旋转设备,你会得到:

INViewController.didRotateFromInterfaceOrientation: 2 to 4
view [UIView]   bounds={{0, …
Run Code Online (Sandbox Code Playgroud)

rotation uiview uiwindow ios

15
推荐指数
1
解决办法
9654
查看次数

ObjC:有没有"类协议"这样的东西?

对于对象实例,我们可以让它们的类声明一些协议一致性,如:

@protocol P <NSObject>
- (void) someMethod ;
@end

@interface C : NSObject <P>
@end

@implementation C
- (void) someMethod {

}
@end
Run Code Online (Sandbox Code Playgroud)

那课怎么样?

我发现自己处于这种情况:

...
Class c = [self modelClass:kind] ;
if (c) {
    model = [c performSelector: @selector(decode:) 
                    withObject: [SExpIO read: [fm contentsAtPath:target]]] ;
}
Run Code Online (Sandbox Code Playgroud)

我希望有一种方法可以让我声明存在类方法协议之类的东西.

在上面的例子中,c的所有类都可以是一个类实例(Hmmm ??),声明

+ (id) decode: (SExp *) root ;
Run Code Online (Sandbox Code Playgroud)

有没有办法可以将上述内容转换为:

if (c) {
    model = [c decode: [SExpIO read: [fm contentsAtPath:target]]]
}
Run Code Online (Sandbox Code Playgroud)

通过使用合适的"类协议"声明?

objective-c

7
推荐指数
2
解决办法
691
查看次数

复制属性和Block_copy(myBlock)/ [myBlock copy]

考虑:

typedef void (^select_block_t)(UIView *) ;

(1) @property (copy, nonatomic) select_block_t        myBlockProperty ;
(2) @property (strong, nonatomic) select_block_t      myBlockProperty ;
(3) @property (assign, nonatomic) select_block_t      myBlockProperty ;
Run Code Online (Sandbox Code Playgroud)

和:

(A) self.myBlockProperty = ^(UIView *) {NSLog(@"Hi");} ;
(B) self.myBlockProperty = [^(UIView *) {NSLog(@"Hi");} copy] ;
Run Code Online (Sandbox Code Playgroud)

我试图了解映射哪个属性声明与块复制语义的正确方法是什么

我看到的例子在这里SO这将有利于[1:B]

但后来我对"复制"操作多么冗余感到困惑.我有限的理解是[1:A]应该是正确的,因为我希望在分配属性时复制块一次,而不是在创建块时再复制一次,然后在属性赋值时再次复制.

根据我的理由,[3:B]也有意义.那么,我有什么误解?

block objective-c ios automatic-ref-counting

7
推荐指数
1
解决办法
808
查看次数

ObjC类标识符和编译器魔术?

考虑:

@interface Foo : NSObject

+ (void) dump ;

@end

@implementation Foo

+ (Class) classOf1 {
    return self ;
}

+ (Class) classOf2 {
    return [Foo class] ;
}

+ (Class) classOf3 {
    return [[[Foo class] new] class] ;
}

+ (Class) classOf4 {
    return [[self new] class] ;
}

+ (Class) classOf5 {
    return [[[self alloc] init] class] ;
}

+ (Class) classOf6 {
    return [[[Foo alloc] init] class] ;
}

+ (Class) classOf7 {
    return [self class] ; …
Run Code Online (Sandbox Code Playgroud)

objective-c

6
推荐指数
2
解决办法
147
查看次数

UIScrollView:contentOffset和bounds.origin有什么区别?

我已经实现了一个UIScrollView委托:

- (void) scrollViewDidScroll: (UIScrollView *) scrollView {
    CGRect bounds = scrollView.bounds ; 
    CGPoint scrollLoc = scrollView.contentOffset ;

    NSLog(@"bounds: %@ offset:%@"
        ,   NSStringFromCGRect(bounds)
        ,   NSStringFromCGPoint(scrollLoc)) ;
}
Run Code Online (Sandbox Code Playgroud)

而且不管我做什么,滚动或旋转的装置,它似乎contentOffsetbounds.origin始终是相同的.

为什么我们需要一个contentOffset,如果它与bounds origin相同,或者两者实际上是不同的情况是什么?

uiscrollview autorotate ios

4
推荐指数
1
解决办法
1万
查看次数

ARC __bridge强制转换Block_copy和Block_release

出于某种原因,我想在运行循环的下一次迭代期间执行一个块,所以我想出了:

typedef void (^resizer_t)() ;

- (void) applyResizer: (resizer_t) resizer {
    resizer() ;
    Block_release(resizer) ;
}

- (void) usage {
    ...
    resizer_t resizer = ^() {
        // stuff
    } ;

    [self performSelectorOnMainThread:@selector(applyResizer:)
                           withObject:(__bridge id) Block_copy((__bridge void *) resizer)
                        waitUntilDone:NO] ;
}
Run Code Online (Sandbox Code Playgroud)
  1. 具有讽刺意味的是,我必须强制转换为Block _copy 的论据 ?
  2. 为什么编译器对我的Block_release感到满意,因为它在没有桥接器void*cast的情况下阻塞Block_copy?

代码似乎工作,我没有发现泄漏也没有过早发布,但我对语法有点困惑......

objective-c ios automatic-ref-counting

4
推荐指数
1
解决办法
5615
查看次数

分配属性,ARC和Core Foundation对象

编辑2.

感谢Ken,现在正在努力.我甚至认为我理解为什么:-)

这是修改后的行:

- (void) reCreatePath {
    CGMutablePathRef p = ::CGPathCreateMutable() ;

    ::CGPathMoveToPoint         (p, 0, TL.x, TL.y) ;
    // [snip]
    ::CGPathAddLineToPoint      (p, 0, BL.x, BL.y) ;
    ::CGPathCloseSubpath(p) ;


    self.path = p ;
    ::CGPathRelease(p) ;   // <<== THIS IS IT!! :-)
}
Run Code Online (Sandbox Code Playgroud)

编辑.

我还是不明白.我试过Chuck的建议:

@property (nonatomic, strong) __attribute__((NSObject)) CGPathRef  path ;
Run Code Online (Sandbox Code Playgroud)

像这样:

@interface TopLeftSlidingView  ()

@property (nonatomic, strong) __attribute__((NSObject)) CGPathRef  path ;

@end
Run Code Online (Sandbox Code Playgroud)

在我重新创建CGPath的时候:

- (void) reCreatePath {
    CGMutablePathRef p = ::CGPathCreateMutable() ;

    ::CGPathMoveToPoint         (p, 0, TL.x, TL.y) ; …
Run Code Online (Sandbox Code Playgroud)

objective-c core-foundation automatic-ref-counting

4
推荐指数
1
解决办法
2596
查看次数

ARC/ObjC++:ObjC容器中的C++对象

考虑:

class SomeCppClass {
public:
    SomeCppClass() {} ;
    ~SomeCppClass() {} ;
} ;

@interface Test1 : NSObject

- (id) init ;

@property (strong, nonatomic) NSMutableArray * container ;

@end

@implementation Test1

@synthesize container ;

- (id) init {
    if (self = [super init]) {
        container = [NSMutableArray arrayWithCapacity:10] ;
        [container addObject:[NSValue valueWithPointer:new SomeCppClass()]] ;
    }
    return self ;
}

- (void) dealloc {
    for (NSValue * v in container) {
        SomeCppClass * c = (SomeCppClass *) [v pointerValue] ;
        delete …
Run Code Online (Sandbox Code Playgroud)

c++ objective-c automatic-ref-counting

3
推荐指数
1
解决办法
1273
查看次数