In the first half of this year, I intend to consolidate the basics and make the most of the doubts in our work and learning. As a rookie level of the programmer, I deeply appreciate the algorithm and the importance of the foundation, if the algorithm is the internal strength, then the basis is the next plate, only the next disk steady, the above will not easily pour. Of course, I deliberately avoid the algorithm here, in fact, I know that I am not research algorithm of material, want to understand some basic algorithm is good, later if encounter great opportunity, learned one or two of the internal strength heart, then I may want to heaven. Haha, I don't know if I can understand the nonsense.
Previously encountered a problem, read data from the database after the discovery of duplicate data, and read the database is to take a one-time reading, in theory, there will be no duplication of data phenomenon. Check the code, found that there is no problem, because of the reason for time, but also to help others solve the problem, so there is no drill down. Finally, a repeat method is written to ensure that the same data is not displayed. The way to repeat is very simple, but the point is not it, I want to say is that if you want to lazy children's shoes to use the Containsobject in the array: The method to repeat will pay attention to, because this method is not as strong as we think. I have two ways to detect what this method is doing.
1. Use a string test. First create several strings, @ "123", @ "456", @ "123", @ "222", and add them to the array. In order to save time, directly into the immutable group, Nsarray *strarray = @[@ "123", @ "456", @ "123", @ "222"]; then create a mutable array, nsmutablearray *teststrarray = [ Nsmutablearray array]; Adds an object in Strarray to Teststrarray, preceded by Containsobject: Determines whether the Teststrarray already exists, and if not, adds. Look at the following code:
1 //1. Comparison of string constants, test object: @ "123", @ "456", @ "123", @ "222"2NSString *STR1 =@"123";3NSString *STR2 =@"345";4NSString *STR3 =@"123";5Nsarray *strarray =@[str1, str2, STR3];6Nsmutablearray *teststrarray =[Nsmutablearray array];7 for(NSString *strinchStrarray)8 {9 if([Teststrarray containsobject:str])Ten { OneNSLog (@"%@ already exists address is%p", str, &str); A } - Else - { theNSLog (@"%@%p", str, &str); - [Teststrarray addobject:str]; - } - } +NSLog (@"%@%p", Teststrarray, &Teststrarray); -NSLog (@"%p", str1); +NSLog (@"%p", str2); ANSLog (@"%p", STR3);
Here's a look at the output:
Look at the bottom three lines, you'll find the first and third addresses are the same, and then add two objects to the array, indicating that this method can repeat the string. So what exactly is to be repeated we don't know, because there are two factors: 1. The string content is the same as @ "123"; 2. The same address. After testing with a custom object, let's say why STR1 and STR3 have the same address, and why the address of STR printed in the For loop is the same.
2. Use a custom object test, like a string, the code is as follows:
1Testobject *testobj1 = [[Testobject alloc] Initwithname:@"123"];2Testobject *testobj2 = [[Testobject alloc] Initwithname:@"1234"];3Testobject *testobj3 = [[Testobject alloc] Initwithname:@"123"];4Testobject *TESTOBJ4 = [[Testobject alloc] Initwithname:@"1235"];5Nsarray *testarray =@[testobj1, TestObj2, TestObj3, TestObj4, testObj1];6Nsmutablearray *testmutablearray =[Nsmutablearray array];7 for(intI=0; I<[testarray count];i++)8 {9Testobject *testobj =Testarray[i];Ten if([Testmutablearray containsobject:testobj]) One { ANSLog (@"%@-------------%p", Testobj, &testobj); - } - Else the { - //NSLog (@ "%@%p%li", Testobj, &testobj, sizeof (testobj)); - [Testmutablearray addobject:testobj]; - } + } -NSLog (@"%@", Testmutablearray); +NSLog (@"1%p", testObj1); ANSLog (@"2%p", testObj2); atNSLog (@"3%p", testObj3); -NSLog (@"4%p", TESTOBJ4);
The results of the output are as follows:
The first object was added two times to remove it, but the same testObj1 and TestObj3 did not repeat, and as a result, it was not possible to distinguish between objects of the same content.
In fact, doing this experiment is to illustrate, Containsobject: is in the comparison of memory address, even if two object content exactly the same, the address is different, that is different. I personally think that this method should be called whether there is the same object, because literally, we may sometimes because we want to lazy, and artificially think of it as content is the same object.
The next thing to say about the string, the immutable string is a very special object that, when initialized, usually points to a string constant, so str = @ "123" indicates that Str holds the @ "123" address, and the str1 and STR3 addresses in the first test are the same. When you print the address of STR in the For loop, the discovery is the same, because STR holds the first address of the array, so it is all the same.
The knowledge of memory address, I am not very clear to the truth, but I will be in the later article, slowly finishing, hope to be able to complete this piece of solution.
IOS Array Containsobject: Methods