0%

抛出问题

群友(@止一)昨晚上发了一个问题:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
typedef struct Test {
NSArray *arr;
} Test;
- (void)test {
Test test = {};
test.arr = @[@1, @2, @3];
self.var = test;
}

- (void)viewDidLoad {
[super viewDidLoad];
[self test];
for (int i = 0; i < 1000; i++) {
NSLog(@"%@", self.var.arr);
}

}

xcode12这样写会崩溃

Read more »

抛出问题

群友(@闲人)贴了这么一个会崩溃的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
@implementation NSObject (test)
- (id)performSelector:(SEL)aSelector withObjects:(NSArray *)objects {

NSMethodSignature *methodSignature = [[self class] instanceMethodSignatureForSelector:aSelector];
// ...
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
// ...
[invocation invoke];
//返回值处理
id callBackObject = NULL;
if(methodSignature.methodReturnLength) {
[invocation getReturnValue:&callBackObject];
}
return callBackObject;
}
@end

@interface TestModel: NSObject
@property (nonatomic, strong) NSNumber *age;
@end
@implementation TestModel
- (instancetype)initWithA:(NSString *)a b:(NSString *)b c:(NSString *)c {
if (self = [super init]) {
printf("initABC\n");
}
return self;
}
@end

@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
TestModel *_teM;
Class class = NSClassFromString(@"TestModel");
_teM = [[class alloc] performSelector:@selector(initWithA:b:c:) withObjects:@[@"1",@"1",@"1"]];
[_teM performSelector:@selector(setAge:) withObject:@1]; /* <- EXC_BAD_ACCESS */
printf("over\n");
}
@end

对,段错误,代码挂在了[_teM performSelector:@selector(setAge:) withObject:@1]

Read more »

一个问题

  • Xcode: Version 11.4.1 (11E503a)
  • swift: Swift 5
1
2
3
4
5
6
7
import UIKit
class MDButton: UIButton {
// 为了更清楚展示问题,代码经过精简
open class func make() -> Self {
return MDButton()
}
}

上面的代码无法编译:

1
2
3
4
ViewController.swift: error: cannot convert return expression of type 'MDButton' to return type 'Self'
return MDButton()
^~~~~~~~~~
as! Self
Read more »