WEBVTT 00:06.450 --> 00:12.870 In this video you're going to learn another iOS 6 feature which will help you write code in a more effective 00:12.870 --> 00:17.150 way with fewer lines of code compared to iOS 5. 00:17.160 --> 00:20.230 This feature is called the structuring. 00:20.250 --> 00:22.660 Now what is this structuring restructuring. 00:22.680 --> 00:28.910 Is it Thomas gives expression that allows us to extract data from arrays objects and so on. 00:30.030 --> 00:34.260 So it can work with arrays and many other types of data. 00:34.300 --> 00:35.480 So we gonna start with arrays. 00:35.490 --> 00:37.100 At the moment. 00:37.560 --> 00:40.860 So let's start with a simple example. 00:40.860 --> 00:42.120 So going back to S5 00:45.520 --> 00:49.000 let's define a couple of variables X and 00:55.740 --> 01:07.320 now in iOS 6 using restructuring you can actually define these two variables in a better way. 01:07.320 --> 01:16.250 So you gonna be fine a constant and that's going to be in the right format and inside the array. 01:16.270 --> 01:25.750 We're going to have our variables X and Y OR A and B and then we're going to assign the array value 01:29.390 --> 01:32.580 to our array made of A and B. 01:32.600 --> 01:42.430 Now if you log a or b you will get 3 and 4. 01:42.680 --> 01:47.250 So this is a simple example of the structuring used with arrays. 01:47.270 --> 01:56.800 Now this will help you in scenarios like when you want to return a process return values in array formats. 01:56.840 --> 01:57.710 So for example 02:03.860 --> 02:15.420 we can define a function process string so this function is gonna take a string and will return and 02:15.580 --> 02:23.030 the right made of the value of the string and the length of the string. 02:23.050 --> 02:31.770 OK so this function is returning in the right now if you want to store the results or the results 02:34.590 --> 02:39.960 in a couple of variables when variables 1 variables storing the value of the other one storing the length 02:40.230 --> 02:45.800 you can use this structuring for that so for example if you log 02:48.590 --> 02:56.540 process a string of yellow you're gonna get an array yellow and white. 02:56.930 --> 03:06.290 Now if you want to store yellow inside a variable value and fly it inside another variable length the 03:06.290 --> 03:14.390 iOS 5 way would be a little bit laborious because you can you're going to store the results inside a 03:14.690 --> 03:22.620 variable result and then you're gonna store value 03:25.420 --> 03:35.240 the value insert like this and then you're going to store the length inside the variable length like 03:35.250 --> 03:40.450 that's right now it is in is six. 03:40.510 --> 03:42.650 It's gonna be a lot easier. 03:43.150 --> 03:56.220 What you can do you can just define these two variables inside an array for months like this and then 03:56.280 --> 04:03.650 you assign that assign the value of the results to this array. 04:03.660 --> 04:04.060 All right. 04:04.080 --> 04:17.630 Now if you log value or lent you get the correct values so this is another example showing how restructuring 04:17.640 --> 04:21.000 can make your coding a lot more effective and easier. 04:21.000 --> 04:25.860 All right now let's take another example where you can use this structuring to make your code a lot 04:25.890 --> 04:27.610 easier. 04:27.630 --> 04:35.900 So imagine this you got a couple of variables see and the 04:40.290 --> 04:43.200 and you want to swap the values of C and the 04:48.570 --> 04:54.580 SO in iOS 5 you had to write a few lines of code to do that. 04:54.630 --> 04:59.580 So first you had to define an intermediate elements. 04:59.920 --> 05:11.360 Is called It's E where you store C on the values of C and then you change the value of C to D. 05:13.020 --> 05:15.420 And then you said the value of the two E. 05:15.500 --> 05:16.770 Now if you if you log 05:19.730 --> 05:29.440 C and the you get to on one leases this is five. 05:29.550 --> 05:36.830 Now in yes 6 you can use structuring for that. 05:37.320 --> 05:39.330 So basically what you can do is 05:46.070 --> 06:00.280 you set the array CND to the values D and C like that's and if you console that log C and D you will 06:00.290 --> 06:02.700 get two and one 06:08.030 --> 06:08.630 can also 06:12.420 --> 06:17.340 instead of rising var c equal 1 Von D equal 1. 06:17.340 --> 06:29.450 You can also use this structuring for that's you can use let's see the equal one and two at the beginning 06:33.890 --> 06:39.400 now there is another feature that you can use with arrays the structuring and that's default values 06:43.320 --> 06:50.520 so let's set the values of two variables city and country using arrays. 06:50.520 --> 06:58.020 The structuring so to do that we're gonna use a format like this city and country and then we're gonna 06:58.020 --> 07:04.460 say but to something like New York and USA. 07:06.630 --> 07:19.050 Now what you can do you can store this inside a variable its call its list. 07:19.430 --> 07:30.410 And if you log your variables CDC and country you get the right results as you would expect. 07:30.410 --> 07:36.330 Now what if inside a list we don't have the country. 07:36.470 --> 07:43.820 We only have the first volume showing the city. 07:43.940 --> 07:51.790 Now if you save the country now you're going to be undefined. 07:51.790 --> 07:52.980 Now what is all this. 07:52.990 --> 07:57.310 You can set a default value to the country. 07:57.370 --> 08:01.330 Let's say that the default value is going to be USA. 08:01.330 --> 08:10.390 So now if you save you get two values one for the city and one for the country. 08:10.390 --> 08:18.520 So now this shows that if you don't provide the second item that is inside the array list then we're 08:18.520 --> 08:20.290 going to use the default value. 08:20.830 --> 08:22.900 But if you specify it. 08:23.940 --> 08:28.120 So for example let's say that's our list now is London and 08:30.840 --> 08:31.740 England. 08:31.890 --> 08:38.870 Then you're gonna get two values that one for the city and one for the country. 08:38.880 --> 08:45.630 So the default value is only gonna be used if you don't provide the corresponding elements inside the 08:46.140 --> 08:48.000 array showing values. 08:48.090 --> 08:54.480 And this is a very important feature in ASX because if you go back to yes five you're going to need 08:54.480 --> 08:57.700 to write a more laborious code. 08:57.840 --> 09:00.420 So let's try that first. 09:00.450 --> 09:06.120 You will need to define the city and set it to the first elements of the array 09:09.600 --> 09:18.290 and then you're going to define the country and you're going to set it to one value or the other depending 09:18.320 --> 09:26.380 on a condition basically you're gonna be checking if the second item is available in the array and then 09:26.380 --> 09:33.750 you're gonna decide whether you said s country to USA defaults while you always says country to the 09:34.170 --> 09:34.840 elements. 09:34.950 --> 09:35.950 If it's available. 09:36.300 --> 09:38.160 So you're gonna be checking if 09:42.100 --> 09:43.090 the second item 09:48.880 --> 09:49.840 is undefined 09:52.430 --> 09:53.090 right. 09:53.100 --> 10:03.320 So if that's the case you use the default value otherwise you use the second element inside the array. 10:03.990 --> 10:13.320 Let's make sure that this is working. 10:13.420 --> 10:17.870 So in this case we've got London and England. 10:17.870 --> 10:19.040 Now if we try 10:24.060 --> 10:27.900 without the second item we use the default value. 10:27.970 --> 10:28.110 Right 10:31.680 --> 10:31.920 all right. 10:31.950 --> 10:34.380 So now is comments the iOS 5 code 10:40.410 --> 10:46.530 so in the next video you're going to learn restructuring with objects.