首页 文章 精选 留言 我的

精选列表

搜索[ios],共10000篇文章
优秀的个人博客,低调大师

iOS深入学习之Weak关键字介绍

前言 从大二的开始接触OC就用到了weak属性修饰词,但是当时只是知道如何去用这个关键字:防止循环引用。根本没有深入地去了解它。 在刚来北京的时候面试过程中也常常考到该知识点。大点的公司可能会问它如何使用?如何在对象销毁后将对象置nil,小点的公司可能只问一下它的使用。 Now,如果你对它产生恐惧或者曾经对它产生过恐惧(+1),如果你被该关键字弄得整天吃不下饭,睡不着觉,那么可以继续往下阅读,希望读过该博客之后能够帮到你。 废话不多说,开始介绍。 由浅入深 先来看看最简单的一个例子: #import "ViewController.h" @interface ViewController () @property (nonatomic,strong)id strongPoint; @property (nonatomic,weak)id weakPoint; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // self.strongPoint = [NSDate date]; self.strongPoint = [[UILabel alloc] init]; self.weakPoint = self.strongPoint; self.strongPoint = nil; NSLog(@"result is :%@", self.weakPoint); } @end 我们可以看到此时输出的结果为: 2017-02-07 13:20:41.119278 Test[7341:2187477] result is :(null) 如果我们使用的strong来修饰weakPoint,此时输出的结果为: 2017-02-07 13:23:13.211164 Test[7344:2187993] result is :<UILabel: 0x100206070; frame = (0 0; 0 0); userInteractionEnabled = NO; layer = <_UILabelLayer: 0x17009a590>> 如果我们使用assign来修饰weakPoint,此时运行程序可能会崩溃(因为如果引用操作发生时内存还没有改变内容,依旧可以输出正确结果,如果引用的时候内存内容发生改变了,就会crash),因为当assign指针所指向的内存被释放之后,不会自动赋值为nil,这样再次引用该指针的时候就会导致野指针操作。 对上述代码运行结果进行分析: 当使用weak关键字的时候,不会增加对象的计数,而且当所指对象置nil的时候,使用weak修饰的指针将被赋值为nil; 当使用strong关键字的时候,会增加对象的计数,也就是说会保持对象值的存在,所以当使用strong的时候weakPoint还会有值。 因此,我们从这里可以得出一个结果: strong是强引用,它会保持对象值的存在; weak是弱引用,当weak指针指向的对象摧毁之后,属性值也会清空(nil out)。 (注意:使用 _ _ weak修饰 和在@ property里面设置weak是一样的) 但是当我们执行如下代码的时候: __strong NSString *yourString = @"Your String"; __weak NSString *myString = yourString; yourString = nil; __unsafe_unretained NSString *theirString = myString; NSLog(@"%p %@", yourString, yourString); NSLog(@"%p %@", myString, myString); NSLog(@"%p %@", theirString, theirString); 你会发现只有yourString为空,其他两个都不为空,这个是为什么呢?原因如下 这里是因为字符字面值永远不会被释放,所以你的weak指针还是指向它。当你使用@""创建一个string对象的时候,它就是一个字面值,永远不会被改变。如果你在程序中很多地方都用到了一样的字符串,那么你可以测试一下,它们都是同一个对象(地址一样),String字面值不会销毁。使用[[NSString alloc] initWithString:@"literal string"]也是一样的效果。因为它指向了一个字面值的string。 那么请问weak指针指向对象被回收的时候该指针是如何被自动置为nil的呢?? 首先,大家可以看一下博客最后面的附录,里面有两个文档,严格来说是Apple的opensouce。里面有一个objc-weak的类。这里是一个objc-weak.h类和一个objc-weak.mm类。 扩展常识 .m和.mm的区别 .m:源代码文件,这个典型的源代码文件扩展名,可以包含OC和C代码。 .mm:源代码文件,带有这种扩展名的源代码文件,除了可以包含OC和C代码之外,还可以包含C++代码。仅在你的OC代码中确实需要使用C++类或者特性的时候才用这种扩展名。 从.h中可以看到以下几个关键的两个结构体:weak_entry_t和weak_table_t,以及一些方法。接下来简单介绍一下weak如何自动置为nil。 weak的实现其实是一个weak表,该表是一个由自旋锁管理的哈希表。 以下是从NSObject.mm里面摘出的一些方法: id objc_initWeak(id *location, id newObj) { if (!newObj) { *location = nil; return nil; } return storeWeak<false/*old*/, true/*new*/, true/*crash*/> (location, (objc_object*)newObj); } 该function的作用是初始化一个新的weak指针指向对象的地址。其中的参数介绍如下: location段:_ _ weak指针的地址 newObj:对象的指针地址 这里调用的storeWeak方法,storeWeak方法里面通过template模板的参数进行更新weak操作,看源码可以知道里面会调用weak_register_no_lock/weak_unregister_no_lock等objc-weak.mm里面的方法进行相应的操作。objc-weak.h里面有句话: For ARR, we also keep track of whether an arbitrary object is being deallocated by briefly placing it in the table just prior to invoking dealloc, and removing it via objc_clear_deallocating just prior to memory reclamation. 对象被废弃时最后调用objc_clear_deallocating。该函数实现如下: void objc_clear_deallocating(id obj) { assert(obj); assert(!UseGC); if (obj->isTaggedPointer()) return; obj->clearDeallocating(); } 也就是调用了clearDeallocating,继续追踪可以发现,它最终是使用了迭代器来取weak表的value,然后调用weak_clear_no_lock,然后查找对应的value,将该weak指针置空: /** * Called by dealloc; nils out all weak pointers that point to the * provided object so that they can no longer be used. * * @param weak_table * @param referent The object being deallocated. */ void weak_clear_no_lock(weak_table_t *weak_table, id referent_id) { objc_object *referent = (objc_object *)referent_id; weak_entry_t *entry = weak_entry_for_referent(weak_table, referent); if (entry == nil) { /// XXX shouldn't happen, but does with mismatched CF/objc //printf("XXX no entry for clear deallocating %p\n", referent); return; } // zero out references weak_referrer_t *referrers; size_t count; if (entry->out_of_line) { referrers = entry->referrers; count = TABLE_SIZE(entry); } else { referrers = entry->inline_referrers; count = WEAK_INLINE_COUNT; } for (size_t i = 0; i < count; ++i) { objc_object **referrer = referrers[i]; if (referrer) { if (*referrer == referent) { *referrer = nil; } else if (*referrer) { _objc_inform("__weak variable at %p holds %p instead of %p. " "This is probably incorrect use of " "objc_storeWeak() and objc_loadWeak(). " "Break on objc_weak_error to debug.\n", referrer, (void*)*referrer, (void*)referent); objc_weak_error(); } } } weak_entry_remove(weak_table, entry); } objc_clear_deallocating该函数的动作如下: 从weak表中获取废弃对象的地址为键值的记录 将包含在记录中的所有附有 _ _ weak修饰符变量的地址,赋值为nil 将weak表中删除该记录 从引用计数表中删除废弃对象的地址为键值的记录 看了objc-weak.mm的源码大概了解了:其实Weak表示一个hash表,然后里面的key是指向对象的地址,Value是Weak指针的地址的数组。 结束 以上便是我个人对weak的理解,查看objc4的源码,发现里面更多的都是结构体、结构体套结构体等等。 附录 Apple的opensource。 Apple的github。 stackoverflow。 原文链接 点此。

优秀的个人博客,低调大师

IPA提交APPStore问题记录(二)iOS10

1、上传安装包后提示:此构建无效 We have discovered one or more issues with your recent delivery for "米饭公社". To process your delivery, the following issues must be corrected: This app attempts to access privacy-sensitive data without a usage description. The app's Info.plist must contain an NSPhotoLibraryUsageDescription key with a string value explaining to the user how the app uses this data. This app attempts to access privacy-sensitive data without a usage description. The app's Info.plist must contain an NSCameraUsageDescription key with a string value explaining to the user how the app uses this data. 解决: 在Info.plist文件中添加相应权限, <key>NSCameraUsageDescription</key> <string>cameraDesciption</string> <key>NSPhotoLibraryUsageDescription</key> <string>photoLibraryDesciption</string>

优秀的个人博客,低调大师

iOS开发技巧 - 使用UISegmentedControl来对信息进行分组

(Swift) import UIKit class ViewController: UIViewController { var segmentedControl:UISegmentedControl! override func viewDidLoad() { super.viewDidLoad() let segments = [ "iPhone", "iPad", "iPod", "iMac"] segmentedControl = UISegmentedControl(items: segments) segmentedControl.center = view.center segmentedControl.addTarget(self, action: "segmentedControlValueChanged:", forControlEvents: .ValueChanged) self.view.addSubview(segmentedControl) } func segmentedControlValueChanged(sender: UISegmentedControl){ let selectedSegmentIndex = sender.selectedSegmentIndex let selectedSegmentText = sender.titleForSegmentAtIndex(selectedSegmentIndex) println("Segment \(selectedSegmentIndex) with text" + " of \(selectedSegmentText) is selected") } } (Objective-C) #import "ViewController.h" @interface ViewController () @property (nonatomic, strong) UISegmentedControl *mySegmentedControl; @end @implementation ViewController ... - (void)viewDidLoad { [super viewDidLoad]; NSArray *segments = [[NSArray alloc] initWithObjects: @"iPhone", @"iPad", @"iPod", @"iMac", nil]; self.mySegmentedControl = [[UISegmentedControl alloc] initWithItems:segments]; self.mySegmentedControl.center = self.view.center; [self.view addSubview:self.mySegmentedControl]; [self.mySegmentedControl addTarget:self action:@selector(segmentChanged:) forControlEvents:UIControlEventValueChanged]; } - (void) segmentChanged:(UISegmentedControl *)paramSender { if ([paramSender isEqual:self.mySegmentedControl]) { NSInteger selectedSegmentIndex = [paramSender selectedSegmentIndex]; NSString *selectedSegmentText = [paramSender titleForSegmentAtIndex:selectedSegmentIndex]; NSLog(@"Segment %ld with %@ text is selected", (long)selectedSegmentIndex, selectedSegmentText); } } (Swift) let segments = NSArray(objects: "Red", UIImage(named: "blueDot")!, "Green", "Yellow") segmentedControl = UISegmentedControl(items: segments) (Objective-C) NSArray *segments = @[ @"iPhone", [UIImage imageNamed:@"iPad"], @"iPod", @"iMac", ]; self.mySegmentedControl = [[UISegmentedControl alloc] initWithItems:segments]; CGRect segmentedFrame = self.mySegmentedControl.frame; segmentedFrame.size.height = 128.0f; segmentedFrame.size.width = 300.0f; self.mySegmentedControl.frame = segmentedFrame; self.mySegmentedControl.center = self.view.center; [self.view addSubview:self.mySegmentedControl];

优秀的个人博客,低调大师

iOS开发技巧 - 使用UISlider来调整值的范围

(Swift) import UIKit class ViewController: UIViewController { var slider: UISlider! func sliderValueChanged(slider: UISlider) { println("Slider's new value is \(slider.value)") } override func viewDidLoad() { super.viewDidLoad() slider = UISlider(frame: CGRect(x: 0, y: 0, width: 200, height: 23)) slider.center = view.center slider.minimumValue = 0 slider.maximumValue = 100 slider.value = slider.maximumValue / 2.0 // customize slider and highlighted with own image slider.setThumbImage(UIImage(named: "ThumbNormal"), forState: .Normal) slider.setThumbImage(UIImage(named: "ThumbHighlighted"), forState: .Highlighted) slider.addTarget(self, action: "sliderValueChanged:", forControlEvents: .ValueChanged) view.addSubview(slider) } } (Objective-C) #import "ViewController.h" @interface ViewController () @property (nonatomic, strong) UISlider *slider; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.slider = [[UISlider alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 200.0f, 23.0f)]; self.slider.center = self.view.center; self.slider.minimumValue = 0.0f; self.slider.maximumValue = 100.0f; self.slider.value = self.slider.maximumValue / 2.0; // customize slider and highlighted with own image [self.slider setThumbImage:[UIImage imageNamed:@"ThumbNormal.png"] forState:UIControlStateNormal]; [self.slider setThumbImage:[UIImage imageNamed:@"ThumbHighlighted.png"] forState:UIControlStateHighlighted]; [self.view addSubview:self.slider]; [self.slider addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged]; }

优秀的个人博客,低调大师

iOS触动精灵模拟触控类外挂原理分析

一、外挂功能: 类似于模拟按键,该类型外挂主要用于通过图像识别,利用luac脚本对图像进行识别。而后再通过私有api实现触屏操作的功能。 二、外挂特征 外挂安装后,会有下面三个主要程序,touchsprite,tsevent,tsdeamon. 其中,touchsprite为gui的界面程序,其主要功能为提供交互界面由用户选择加载的脚本。(脚本保存在/User/Media/TouchSprite/lua中) 三、外挂实现 另外两个程序则为实现模拟触控的核心程序。 其中tsdeamon为守护进程,监控手机的音量的“-”按键事件(用于弹出是否运行脚本的窗口),以及负责解析lua脚本。并将脚本中的触控事件通过共享内存,交由tsevent处理 而tsevent则主要负责处理模拟触控信息,其利用苹果的一系列私有api: 实现模拟触控的功能

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

用户登录
用户注册
修改密码
忘记密码