nsarray

Xcode auto-complete suggests mysterious “songsAtIndexes” for NSArray getter

≡放荡痞女 提交于 2020-06-27 16:25:10
问题 I have a property of type NSArray on my class called "songs". I'm creating a custom getter for it and XCode gives me an option of creating a method: songsAtIndexes:(NSIndexSet *)indexes What is this and why is XCode offering this? Is this specific to NSArray properties? What is the purpose of creating a method/getter for this method? If I don't define it manually, will it be automatically created/synthesized? 回答1: This is the result of a little-used KVC optimization for indexed collections

Swift: Check which value in NSArray is closest to another given value

有些话、适合烂在心里 提交于 2020-05-16 05:01:28
问题 Lets say I have an array var values:[CGFloat] = [-12.0, 450, 300] I need to find out which of these numbers is closest to a given value, say var givenValue:CGFloat = 64 Is there an efficient way to find out which object in the array is closest to 64? I know you can do something like this: if abs(values[0] - 64) < abs(values[1] - 64) && abs(values[0] - 64) < abs(values[2] - 64) { println("values[0] is the closest to 64) } But this will result in several if-statements and seems inefficient.

NSArray NSMutableArray的简单使用

孤街醉人 提交于 2020-03-01 03:28:48
一:数组介绍: 1、定义: 简单地说就是元素的集合。OC中数组可以保存不同类型的对象,但只能保存对象。不能保存基本类型,若保存基本数据类型 (int double float ....),需要转换成对象(NSString NSNumber...)保存。数组的最后一个元素为是nil,表示结束。 2、分类: 可变数组 NSMutableArray 不可变数组 NSArray (可不可变是指个数能不能改变,数组元素的值还是可以改变的) 二:使用: 1、初始化: NSArray *array = [[NSArray alloc] initWithObjects:@"one",@"two",@"three",@"four", nil]; NSArray *array2 = [NSArray arrayWithObjects:@"1",@"2",@"3", nil]; // 不同类型的元素 NSArray *array3 = [NSArray arrayWithObjects:@"1",array,@"2",array2,@"3", nil]; // 通过一个数组,来创建一个数组! NSArray *array4 = [[NSArray alloc] initWithArray:array]; NSArray *array5 = [NSArray arrayWithArray:array];

NSPredicate on nested array with NSDictionary as object

心已入冬 提交于 2020-02-16 05:25:49
问题 i have a NSDictionary like: { "2017-05-02" = ( { "always_valid" = 0; date = "2017-05-02"; from = "12:00"; to = "13:00"; }, { "always_valid" = 0; date = "2017-05-02"; from = "12:00"; to = "12:00"; }, { "always_valid" = 0; date = "2017-05-02"; from = "14:00"; "hourly_rate" = 12; to = "15:00"; } ); "2017-05-03" = ( { "always_valid" = 0; date = "2017-05-03"; from = "12:00"; to = "13:00"; } ); "2017-05-18" = ( { "always_valid" = 1; date = "2017-05-18"; from = "12:00"; to = "12:00"; } ); } i'm

NSPredicate on nested array with NSDictionary as object

假装没事ソ 提交于 2020-02-16 05:25:24
问题 i have a NSDictionary like: { "2017-05-02" = ( { "always_valid" = 0; date = "2017-05-02"; from = "12:00"; to = "13:00"; }, { "always_valid" = 0; date = "2017-05-02"; from = "12:00"; to = "12:00"; }, { "always_valid" = 0; date = "2017-05-02"; from = "14:00"; "hourly_rate" = 12; to = "15:00"; } ); "2017-05-03" = ( { "always_valid" = 0; date = "2017-05-03"; from = "12:00"; to = "13:00"; } ); "2017-05-18" = ( { "always_valid" = 1; date = "2017-05-18"; from = "12:00"; to = "12:00"; } ); } i'm

YAJL memory leak problem array

余生颓废 提交于 2020-02-07 06:46:31
问题 I am trying to fetch JSON data every 2 seconds and pass it to another class for processing, everything works fine but the below code seems to have memory leaks (from Instruments) but I cannot figure out what is wrong and how I can fix, can someone please advise ??? * Updated with the full logic and it looks like the array that is passed on to the main method is leaking and Instruments is falsely reporting that as YAJL leak..(not very sure thou) * @property (nonatomic,retain,readwrite)

NSArray sortedArrayUsingSelector memory leak

夙愿已清 提交于 2020-01-24 14:02:53
问题 I receive a memory leak for the line containing the sortedArrayUsingSelector definition. Does anybody know what might be the problem? @property (nonatomic, retain) NSArray *indexLetters; ... NSMutableDictionary *indexedCategories = [[NSMutableDictionary alloc] init]; ... self.indexLetters = [[indexedCategories allKeys] sortedArrayUsingSelector:@selector(compare:)]; [indexedCategories release]; 回答1: It could be because you're not releasing the indexLetters variable in dealloc . 来源: https:/

Cannot initialize a parameter of type 'id _NonNull' with an lvalue of type double

孤者浪人 提交于 2020-01-24 08:47:26
问题 Objective C: I have multiple variables of type double, long long, NSString and int which I want to put in an array to be printed as a single line in a CSV file NSArray *ValArray = [NSArray arrayWithObjects: var1, var2, var3 , var4, var5, nil]; Here var1 is of type double and var2,var3 are of the type long long. This gives me a syntax error saying that "Cannot initialize a parameter of type 'id _NonNull' with an lvalue of type double" at var1 I'm a newbie in Objective C and unable to figure

Finding a particular instance in an array by a property

╄→尐↘猪︶ㄣ 提交于 2020-01-20 08:52:27
问题 I have an array containing various instances of MyClass , which has four properties: int id , int valueA , int valueB , and int valueC . On occasion, I need to modify a property for a specific instance (for this example let's say it is the one with id equal to 5 ). Currently I do it like this: MyClass *myClass = [[MyClass alloc] init]; for (int i = 0; i < [myMutableArray count]; i++) { myClass = [myMutableArray objectAtIndex:i]; if(myClass.id == 5) { myClass.valueA = 100; myClass.valueB = 200