小编use*_*037的帖子

Objective-C - ARC - NSNumber - 分段故障

我有一个Objective-C程序,我正在使用ARC(自动引用计数),它在第23行引发了一个分段错误(参见下面的程序).

问题 1)为什么会发生分段错误?

以下是该计划:

#import<Foundation/Foundation.h>

@interface Car : NSObject
@property (weak) NSNumber* doors;
@end

@implementation Car 
@synthesize doors;
@end

int main()
{
    system("clear");

    @autoreleasepool
    {    
        Car *car1 = [[Car alloc] init];

        printf("1\n");
        NSNumber *d1 = [[NSNumber alloc] initWithInteger: 4]; 

        printf("2\n");
        car1.doors = d1;   //Segmentation fault.. why ?

        printf("3\n");
    }   

    printf("---- end\n");

    return(0);
}
Run Code Online (Sandbox Code Playgroud)

输出:

1
2
Segmentation fault: 11
Run Code Online (Sandbox Code Playgroud)

weak-references objective-c segmentation-fault nsnumber automatic-ref-counting

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

财产声明 - ivar和getter值不匹配

我对财产重新申报表示怀疑

概述:

  • class"A"是具有readonly属性int n1的父类;
  • class"B"是子类,它将属性重新声明为读写
  • 使用类"B"的setter,属性值设置为20
  • 当我使用getter和实例变量打印值时,我似乎得到不同的值

注意事项: - 内存管理= ARC(自动引用计数)

题:

  • 当我打印self.n1和_n1的值时,为什么我会得到不同的值?
  • 我的预期行为和实际行为与原因不符(请向下滚动以查看实际与预期的对比)?

代码:(在单独的文件中)

#import<Foundation/Foundation.h>

@interface A : NSObject

@property (readonly) int n1;

- (void) display;

@end
Run Code Online (Sandbox Code Playgroud)

上午

#import "A.h"

@implementation A

@synthesize n1 = _n1;

- (void) display
{
    printf("_n1     = %i\n", _n1);                  //I expected _n1 and self.n1 to display the same value
    printf("self.n1 = %i\n\n", self.n1);            //but they seem to display different values
}

@end
Run Code Online (Sandbox Code Playgroud)

BH

#import"A.h"

@interface B : A

@property (readwrite) int n1;

@end …
Run Code Online (Sandbox Code Playgroud)

inheritance properties objective-c redeclaration

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

阻止声音(辅助功能)宣布UITableViewCell为选中状态

当选择UITableViewCell时,语音通过宣布"已选择 ",我不希望语​​音结束说"已选择".我怎样才能实现这一目标?

我尝试过的事情没有成功:

  • 改变了细胞accessibilityHintaccessibilityLabel
  • 改变了细胞 selectionStyle = UITableViewCellSelectionStyleNone
  • 改变了细胞 accessibilityTraits = UIAccessibilityTraitButton

题:

  • 当选择单元格时,我不希望语​​音结束说"已选择".我怎样才能实现这一目标?

accessibility uitableview ios voiceover uiaccessibility

10
推荐指数
2
解决办法
2195
查看次数

dequeueReusableCellWithIdentifier始终返回nil(不使用storyboard)

我正在使用重用标识符以编程方式创建单元格.

注意 - 我没有使用故事板来创建单元格

每当单元出列时,单元格为零,因此需要使用alloc重新创建单元,这很昂贵.

编辑(增加了1个问题并更正了代码)

  • 为什么这个出列总是返回零?我该如何纠正?
  • 只有在与storyboard/nib文件一起使用时才会出列队列吗?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(!cell) //Every time cell is nil, dequeue not working 
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    }

    return cell;
}
Run Code Online (Sandbox Code Playgroud)

objective-c deque uitableview ios

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

CNContact上次修改日期和联系人已更改

问题:

  1. 如何获取CNContact中的最后修改日期(新的苹果联系框架)?
    keysToFetch列表中不存在最后修改日期

  2. 我想获取并更新自上次提取以来已修改的联系人.我怎样才能做到这一点 ?

ios cncontact

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

withThrowingTaskGroup - “try”表达式中不会调用抛出函数

问题:

我有以下显示警告的功能No calls to throwing functions occur within 'try' expression

问题:

  • 为什么会显示此警告?(任务内的代码抛出错误)
  • 我应该怎么做才能将错误传播给 的调用者f1

代码:

func f1() async throws {
    try await withThrowingTaskGroup(of: Int.self) { group in //No calls to throwing functions occur within 'try' expression
        group.addTask(priority: .high) {
            throw NSError()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

ios async-await swift swift-concurrency

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

将包依赖项编辑为本地包

概述

  • 我有一个使用远程 swift 包的项目。
  • 我想在同一个项目中编辑这个包,以便我可以根据使用情况对其进行改进。

目的:

  • 为此,我尝试添加与本地包相同的包,以便我可以编辑它。

参考:

https://developer.apple.com/documentation/swift_packages/editing_a_package_dependency_as_a_local_package

问题:

  • 当我将包含的克隆文件夹拖放Package.swift到项目中时,我看不到项目中的任何本地包源文件。(见截图)

带有 Swift 包的 Xcode 项目

我一直为此伤透了脑筋。对此的任何帮助将不胜感激。

Xcode版本:

  • 13.2 (13C90)(从开发者门户下载,而不是AppStore)

xcode swift swift-package-manager swift-package

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

跨子导航控制器传递数据

概观

我有一个iOS项目,其中包含2个导航控制器,如下图所示.

当它从AAA切换到CCC时我想传递一些数据但是在AAA和CCC之间有一个导航控制器.

在此输入图像描述

根据Apple的文档,UINavigationController不应该是子类,因此我无法创建委托并传递数据.

题:

  • 如何将数据从AAA传递到CCC?
  • 任何工作来实现这一目标?

uinavigationcontroller ios segue

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

特定类的Swift协议

我想定义一个UIViewController符合哪种类型的变量P1.

题:

  • 我应该修改协议还是v1类型?
  • 我该怎么做?

码:

protocol P1 {

    func f1();
}


var v1 : P1 //But needs to be a UIViewController which conforms to `P1`
Run Code Online (Sandbox Code Playgroud)

protocols swift

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

UISearchController - 搜索栏应始终可见

目标:使搜索栏始终可见.

  1. 视图控制器嵌入在导航控制器中
  2. 不存在表视图
  3. 当用户点击搜索栏时,搜索栏需要从其当前位置动画到导航栏的顶部(向上移动),就像在iOS联系人应用中一样.
  4. 使用 UISearchController

我做了什么:

  1. 我已将搜索栏添加到视图控制器的视图中
  2. 我手动呈现搜索视图控制器(下面的代码)

问题:

  • 我无法控制动画,目前搜索栏从屏幕顶部向下移动到导航栏的位置.

预期行为:

  • 能够通过将搜索栏从当前位置向上移动到导航栏来为搜索栏设置动画

  1. 我的方法是否正确?
  2. 如何设置动画并从当前位置向上移动搜索栏?

码:

    func setupSearchController() {
        searchController.searchResultsUpdater = self
        self.definesPresentationContext = false
        searchController.delegate = self
        searchController.hidesNavigationBarDuringPresentation = true

        let searchBar = searchController.searchBar

        view.addSubview(searchBar)

        searchBar.translatesAutoresizingMaskIntoConstraints = false

        searchBar.leadingAnchor.constraintEqualToAnchor(view.leadingAnchor).active = true
        searchBar.trailingAnchor.constraintEqualToAnchor(view.trailingAnchor).active = true
        searchBar.topAnchor.constraintEqualToAnchor(topLayoutGuide.bottomAnchor).active = true

    }

    func presentSearchController(searchController: UISearchController) {
        let searchBar = searchController.searchBar

        searchBar.removeFromSuperview()

        let baseView = searchController.view

        baseView.addSubview(searchBar)

        searchBar.leadingAnchor.constraintEqualToAnchor(baseView.leadingAnchor).active = true
        searchBar.trailingAnchor.constraintEqualToAnchor(baseView.trailingAnchor).active = true
        searchBar.topAnchor.constraintEqualToAnchor(searchController.topLayoutGuide.bottomAnchor).active …
Run Code Online (Sandbox Code Playgroud)

uitableview ios swift uisearchcontroller

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