小编hoa*_*Cap的帖子

Objective-C:弱的attritube不能按预期工作

可能重复:
为什么iOS中没有释放弱NSString属性?

我是Objective C的新手,我有一些问题,我无法自己回答.我有一段用于测试__weak变量的代码(我当然使用ARC):

NSString *myString = [[NSString alloc] initWithFormat:@"John"];
NSString * __weak weakString = myString;
myString = nil; //<-- release the NSString object
NSLog(@"string: %@", weakString);
Run Code Online (Sandbox Code Playgroud)

上述代码的输出是预期的,因为weakString是一个弱变量:

2013-01-02 11:42:27.481 ConsoleApp[836:303] string: (null)
Run Code Online (Sandbox Code Playgroud)

但是当我将代码修改为:

NSString *myString = [[NSString alloc] initWithFormat:@"John"];
NSString * __weak weakString = myString;
NSLog(@"Before: %@", weakString); //<--- output to see if the __weak variable really works.
myString = nil;
NSLog(@"After: %@", weakString);
Run Code Online (Sandbox Code Playgroud)

输出完全不是我的预期:

2013-01-02 11:46:03.790 ConsoleApp[863:303] Before: John
2013-01-02 11:46:03.792 ConsoleApp[863:303] After: John
Run Code Online (Sandbox Code Playgroud)

后一个NSLog的输出必须是(nil)而不是"John".我试图在许多文件中搜索,但我没有找到这个案例的答案.有人可以给出合理的解释吗?提前致谢.

weak-references ios strong-references

10
推荐指数
1
解决办法
448
查看次数

XCode 9.1 - 缺少位置模拟

我今天刚刚将我的XCode更新为9.1,令人惊讶的是我在方案设置中找不到"允许位置模拟",请参考下面的截图:

在此输入图像描述

有没有人遇到同样的问题?还有什么建议我可以解决它吗?

苹果最近变得越来越糟.

simulation location xcode9.1

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

显示警报视图时,长按手势会被调用两次

我在这里遇到了长按手势的麻烦.我环顾四周,发现了一些与我的问题有关的帖子,但直到现在还没有运气.

我有一个长按手势的视图,我想在手势触发时显示警报视图,但不知何故,当显示警报视图时触发器被调用两次,我检查手势识别器的状态但仍然没运气.这里的代码:

初始代码:

UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];

[longTap setMinimumPressDuration:1];
[self addGestureRecognizer:longTap];


- (IBAction)handleTapGesture:(UIPanGestureRecognizer*)sender {
if (sender.state == UIGestureRecognizerStateChanged) {
NSLog(@"Change");
} else if (sender.state == UIGestureRecognizerStateEnded) {
NSLog(@"Ended");
}
else {
NSLog(@"Begin");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Long pressed" message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show]; //If I remove this line, the trigger is call only once. 
}
}
Run Code Online (Sandbox Code Playgroud)

奇怪的是,如果我删除[警报显示],一切都按预期进行,手势只触发一次.有人对此有解释吗?提前致谢.

objective-c uigesturerecognizer

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

Swift 4 - 扩展中的私有方法

我正在寻找一种在扩展中编写私有函数的方法。

例如:

class A: UIViewController {
    override viewDidLoad() {
        privateFoo()
    }
}

private extension A {
    func foo() { 
        privateFoo()
    }

    private func privateFoo() { //Helper function for foo(), expected to be called inside the scope of this extension only

    }
}
Run Code Online (Sandbox Code Playgroud)

但是,即使我将 privateFoo() 声明为private,我仍然可以从扩展外部调用它,这不是我所期望的。

你能帮助我如何实现我的目标吗?

methods private swift

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

Swift - 不同表视图单元格的通用模型

我正在开发一个表格视图(类似于 Facebook Messenger 的表格视图),其中有不同的单元格类型(图像、文本、视频等)。我想要存档的是,我想声明一个列表message model,我将配置tableView,以便单元格将由模型本身确定和配置。为了做到这一点,我需要以某种方式告诉它关联的是model哪个UITableViewCell类。基本上我想要一个像这样的协议:

protocol ChatMessageDisplayable {
   static var myCellType: UITableViewCell { get } //Defines the UITableViewCell class this model is associated with
   func configure(cell: /*the associated cell type*/) // Let the model itself configure the cell.
}
Run Code Online (Sandbox Code Playgroud)

然后我在我的 ViewController 中声明一个数组

messageModels = [ChatMessageDisplayable]

我的 UITableViewDataSource 实现:

public func numberOfSections(in tableView: UITableView) -> Int {
    return messageModels.count
}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let model …
Run Code Online (Sandbox Code Playgroud)

generics uitableview swift

0
推荐指数
1
解决办法
4023
查看次数