Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

iOS_course_7

437 views

Published on

iOS Course lection 7
NSArray, NSDictionary, Blocks, KVC, KVO.

Published in: Software
  • DOWNLOAD THE BOOK INTO AVAILABLE FORMAT (New Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://urlzs.com/UABbn } ......................................................................................................................... Download Full EPUB Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download Full doc Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download PDF EBOOK here { https://urlzs.com/UABbn } ......................................................................................................................... Download EPUB Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download doc Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THE can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer THE is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBOOK .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, CookBOOK, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, EBOOK, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story THE Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money THE the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths THE Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD THE BOOK INTO AVAILABLE FORMAT (New Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://urlzs.com/UABbn } ......................................................................................................................... Download Full EPUB Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download Full doc Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download PDF EBOOK here { https://urlzs.com/UABbn } ......................................................................................................................... Download EPUB Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download doc Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THE can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer THE is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBOOK .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, CookBOOK, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, EBOOK, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story THE Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money THE the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths THE Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD THE BOOK INTO AVAILABLE FORMAT (New Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://urlzs.com/UABbn } ......................................................................................................................... Download Full EPUB Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download Full doc Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download PDF EBOOK here { https://urlzs.com/UABbn } ......................................................................................................................... Download EPUB Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download doc Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THE can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer THE is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBOOK .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, CookBOOK, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, EBOOK, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story THE Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money THE the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths THE Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD THE BOOK INTO AVAILABLE FORMAT (New Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download Full EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download Full doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THE can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer THE is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBOOK .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, CookBOOK, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, EBOOK, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story THE Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money THE the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths THE Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD THE BOOK INTO AVAILABLE FORMAT (New Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download Full EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download Full doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download PDF EBOOK here { https://soo.gd/irt2 } ......................................................................................................................... Download EPUB Ebook here { https://soo.gd/irt2 } ......................................................................................................................... Download doc Ebook here { https://soo.gd/irt2 } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THE can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer THE is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBOOK .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, CookBOOK, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, EBOOK, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story THE Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money THE the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths THE Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

iOS_course_7

  1. 1. iOS: Sviluppo App iPhone & iPad Dario Pizzuto 1
  2. 2. Objective-C - 3a parte 2
  3. 3. Objective-C 3 Argomenti • Array • Dictionary • Blocks • Exceptions • Key Value Coding • Key Value Observing
  4. 4. Array 4
  5. 5. Objective-C 5 Array • Struttura dati composta da un numero predeterminato di variabili dello stesso tipo • Objective-C ci fornisce la classe immutabile NSArray e la classe mutabile NSMutableArray • Non possono contenere elementi nil, si usa NSNull
  6. 6. Objective-C 6 Esempio NSArray NSArray *people = [[NSArray alloc] init] Inizializzazione con oggetti NSArray *people = [[NSArray arrayWithObjects:obj1, obj2, nil]; oppure NSArray *people = @[obj1,obj2];
  7. 7. Objective-C 7 NSMutableArray • Ci permette di manipolare l’array perché un oggetto mutable • Es ✓[mutablePeople insertObject:obj1 atIndex:2]
  8. 8. Dictionary 8
  9. 9. Objective-C 9 Dictionary • Struttura dati che ci permette di memorizzare oggetti chiave - valore • Molto efficiente • Objective-C ci fornisce la classe immutabile NSDictionary e la classe mutabile NSMutableDictionary • Non possono contenere elementi nil, si usa NSNull
  10. 10. Objective-C 10 Esempio NSDictionary NSDictionary *dictPeople = [[NSDictionary alloc] init] Inizializzazione con oggetti NSDictionary *dictPeople = [[NSDictionary dictionaryWithObjectsAndKeys:obj1, key1, obj2, key2, nil]; oppure NSDictionary *dictPeople = @{key1:obj1,key2:obj2};
  11. 11. Objective-C 11 NSMutableDictionary • Ci permette di manipolare il dizionario perché è un oggetto mutable • Es ✓[mutableDictPeople removeObjectforKey:key1]
  12. 12. Let’s Code 12
  13. 13. Objective-C 13 Let’s Code ✦Implementare un array che contenga i nomi dei ragazzi del corso ✦Implementare un dictionary che contenga come chiave il numero progressivo assegnato al vostro nome e come valore il nome
  14. 14. Blocks 14
  15. 15. Objective-C 15 Block • Insieme di codice in linea • Tipizzato • Si usano per comporre espressioni che possono essere passate come parametri
  16. 16. Objective-C 16 Block • Sono particolarmente indicati come gestori di Callback • Un block può accedere alle variabili locali • La sintassi non è intuitiva • Disponibili da iOS 4
  17. 17. Objective-C 17 Dichiarazione Block • Prevede l’uso del carattere “^” (returnType)^(parameters); • returnType: tipo di valore di ritorno • parameters: parametri del blocco
  18. 18. Objective-C 18 Esempio Block int (somma)^(int a, int b); • Blocco che ritorna la somma tra a e b
  19. 19. Objective-C 19 Esempio Block completo
  20. 20. Objective-C 20 Block • I blocchi possono leggere e modificare variabili che stanno fuori dal blocco • Utilizzo della keyword __block prima del tipo di variabile
  21. 21. Objective-C 21 Esempio block sola lettura
  22. 22. Objective-C 22 Errore Block
  23. 23. Objective-C 23 Corretto
  24. 24. Objective-C 24 Block utilizzo • È buona norma definire un blocco con typedef • Può essere utilizzato “in linea” o come parametro di un metodo
  25. 25. Objective-C 25 Block in linea typedef double (^area) (double lato); area areaQuadrato = ^(double lato){ return lato*lato; };
  26. 26. Objective-C 26 Esempio Block in linea
  27. 27. Objective-C 27 Block parametro metodo - (void)countString:(NSString*)match array:(NSArray*)arrayOfString counter:(void(^)(int counter))counter{ __block int count = 0; [arrayOfString enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { if([(NSString*)obj isEqualToString:match]){ count+=1; } }]; counter(count); }
  28. 28. Objective-C 28 Esempio Block parametro metodo
  29. 29. Exceptions 29
  30. 30. Objective-C 30 Exceptions • Sono delle interruzioni del programma dovute ad errori non previsti • Quando questo accade, viene “sollevata un’eccezione”
  31. 31. Objective-C 31 Exceptions • Objective-C ci fornisce degli strumenti per gestire gli errori imprevisti in 3 parti 1. Istruzioni sottoposte a controllo 2. Routine di controllo dell’errore 3. Blocco di codice eseguito sempre
  32. 32. Objective-C 32 Exceptions @try [instructions] [@catch(specific exceptions)] [@finally [instructions] ]
  33. 33. Objective-C 33 Blocco try-catch-finally • try: istruzioni da controllare • catch: gestione dell’errore generico o specifico • finally: insieme di istruzioni eseguite sempre
  34. 34. Objective-C 34 Exceptions • Le eccezioni generiche vengono identificate con la classe NSException • Tutti i tipi di eccezione in Objective-C sono sottoclassi di NSException
  35. 35. Objective-C 35 Esempio exceptions @try { NSArray *names = @[@"Dario",@"Mara",@"Pino"]; NSLog(@"Name: %@",[names objectAtIndex:3]); } @catch (NSException *exception) { NSLog(@"Exception: %@nReason: %@“, [exception name], [exception reason]); }
  36. 36. Objective-C 36 Esempio exception
  37. 37. Objective-C 37 Exceptions non gestite • Nel caso non ci sia il blocco try-catch per gestire l’eccezione, il programma si interromperà (crash) e vedremo nella console lo stacktrace completo
  38. 38. Objective-C 38 Esempio exception non gestita
  39. 39. Objective-C 39 Sollevare exceptions • Alcune volte è utile generare un’eccezione • Soprattutto quando realizziamo nostre API • ES. ✓Parametri metodo a nil ✓Funzionalità non prevista da quel determinato metodo
  40. 40. Objective-C 40 Sollevare exceptions • Utilizziamo la direttiva @throw @throw [NSException exceptionWithName:@”Errore parametro” reason:@”Variabile nulla” userInfo:nil]
  41. 41. Objective-C 41 Exceptions personalizzate • Se vogliamo definire una NSException personalizzata, dobbiamo creare una sottoclasse di quest’ultima @interface DPPRangeException : NSException { ... }
  42. 42. Objective-C 42 Catturare exceptions multiple • È possibile intercettare, a cascata, eccezioni multiple • Definiamo n @catch clause per quante tipi di eccezioni vogliamo catturare
  43. 43. Objective-C 43 Exceptions multiple @try { NSArray *names = @[@"Dario",@"Mara",@"Pino"]; NSLog(@"Name: %@",[names objectAtIndex:3]); } @catch (DPPStringException *ex) { NSLog(@"Exception: %@nReason: %@",[exception name], [exception reason]); } @catch (NSException *ex){ NSLog(@"Exception generica: %@nReason: %@",[exception name], [exception reason]); }
  44. 44. Objective-C 44 Clausola finally • Il blocco di codice all’interno della clausola finally viene eseguito sempre in seguito alla valutazione del blocco try • Nel finally, troviamo spesso operazioni di di rilascio di risorse • Se apro un file, è inserisco nel @try le operazioni di apertura e lettura, posso sfruttare il @finally per chiudere il buffer
  45. 45. Objective-C 45 Esempio finally ..... @try { [self openFile]; @throw [NSException exceptionWithName:@”Errore lettura” reason:@”File non esistente” userInfo:nil]; [self closeFile] //Mai eseguita } @catch (NSException *ex) { NSLog(@"Exception: %@nReason: %@",[exception name], [exception reason]); } @finally { [self closeFile]; //Chiudo lo stream }
  46. 46. Let’s Code 46
  47. 47. Objective-C 47 Let’s Code ✦Implementare un blocco try catch finally che gestisca l’eccezione sollevata a runtime quando viene ricercato un elemento in una posizione inesistente di un array
  48. 48. Key Value Coding 48
  49. 49. Objective-C 49 Key Value Coding - KVC • Protocol che permette l’accesso alle properties in modo diretto tramite key • Le keys corrispondono ai nomi delle property • Metodi dichiarati in NSKeyValueCoding • Buon principio di design del codice • Permette di semplificare il codice
  50. 50. Objective-C 50 Key Value Coding - KVC • Metodo valueForKey: ✓Ritorna il valore per la key specificata • Se la key non viene trovata, viene chiamato il metodo valueForUndefinedKey: ✓Solleva un’eccezione di tipo NSUndefinedKeyException
  51. 51. Objective-C 51 Esempio codice - (id)tableView:(NSTableView *)tableview objectValueForTableColumn:(id)column row:(NSInteger)row { ChildObject *child = [childrenArray objectAtIndex:row]; if ([[column identifier] isEqualToString:@"name"]) { return [child name]; } if ([[column identifier] isEqualToString:@"age"]) { return [child age]; } if ([[column identifier] isEqualToString:@"favoriteColor"]) { return [child favoriteColor]; } }
  52. 52. Objective-C 52 Esempio codice con KVC - (id)tableView:(NSTableView *)tableview objectValueForTableColumn:(id)column row: (NSInteger)row { ChildObject *child = [childrenArray objectAtIndex:row]; return [child valueForKey:[column identifier]]; }
  53. 53. Objective-C 53 Key Value Coding - KVC • Permette anche il set delle properties di una classe • Si usa setValue:forKey:
  54. 54. Objective-C 54 Esempio codice con KVC MyClass *myInstance = [[MyClass alloc] init]; [myInstance setValue:@2 forKey:@"integerProperty"];
  55. 55. Let’s Code 55
  56. 56. Objective-C 56 Let’s Code ✦Implementare il get e set di una property di una classe a piacere attraverso il KVC
  57. 57. Key Value Observing 57
  58. 58. Objective-C 58 Key Value Observing - KVO • Fornisce la possibilità ad un oggetto, di ricevere una notifica quando varia la property di un altro oggetto • Utile per gestire cambi di visualizzazione o azioni all’interno della nostra App • Funzionalità specifica di Objective-C
  59. 59. Objective-C 59 Key Value Observing - KVO • Supponiamo di avere la seguente situazione • Vogliamo sapere quando il nostro accountBalance varia
  60. 60. Objective-C 60 Key Value Observing - KVO • l’oggetto PersonObject, registra un observer sulla property accountBalance • Metodo addObserver:forKeyPath:options:context:
  61. 61. Objective-C 61 Key Value Observing - KVO • Per ricevere la notifica, l’osservatore deve implementare il metodo observeValueForKeyPath:ofObject:change:context:
  62. 62. Objective-C 62 Key Value Observing - KVO • Quando avviene il cambio di valore nell’attributo accountBalance, automaticamente viene notificato all’osservatore
  63. 63. Objective-C 63 Key Value Observing - KVO • Rimuovere observer quando viene deallocata la classe altrimenti potremmo generare dei leaks • Metodo removeObserver:forKeyPath:
  64. 64. Objective-C 64 Key Value Observing - KVO • Non dobbiamo specificare che tipo di notifica inviare, iOS gestisce autonomamente la comunicazione • Non dobbiamo implementare schemi particolari di gestione per intercettare le modifiche • Semplice da adottare
  65. 65. Objective-C 65 Ulteriori approfondimenti NSArray reference NSDictionary reference Getting started with blocks How do I declare a block in Objective-C ?
  66. 66. Objective-C 66 Ulteriori approfondimenti NSException reference Key Value Coding Key Value Observing
  67. 67. Domande ? 67

×