0Pricing
Objective-C iOS Development for Legacy & Enterprise Apps · Lekcja

Przechowywanie preferencji za pomocą NSUserDefaults

Trwale przechowuj lekkie ustawienia i preferencje użytkowników w aplikacjach Objective-C za pomocą NSUserDefaults, najprostszego magazynu klucz-wartość w iOS.

Przechowywanie preferencji za pomocą NSUserDefaults to bezpłatna lekcja Objective-C iOS Development for Legacy & Enterprise Apps na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Objective-C iOS Development for Legacy & Enterprise Apps, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Objective-C iOS Development for Legacy & Enterprise Apps zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

What is NSUserDefaults?

NSUserDefaults is a built-in key-value store for small pieces of data — settings, flags, and preferences. It persists automatically across app launches.

When to Use It

Use it for small, simple values:

  • User toggles (dark mode on/off)
  • Last selected tab
  • Onboarding-completed flag

Do NOT use it for large data or sensitive secrets.

The Standard Instance

Access the shared store through standardUserDefaults.

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

Writing Values

Set values by key with typed setters.

[defaults setBool:YES forKey:@"darkMode"];
[defaults setInteger:5 forKey:@"launchCount"];
[defaults setObject:@"Ada" forKey:@"username"];

Reading Values

Read with matching getters. Missing keys return sensible defaults (NO, 0, nil).

BOOL dark = [defaults boolForKey:@"darkMode"];
NSInteger count = [defaults integerForKey:@"launchCount"];
NSString *name = [defaults stringForKey:@"username"];

Supported Types

NSUserDefaults stores property-list types only:

  • NSString, NSNumber, BOOL, integers, doubles
  • NSArray, NSDictionary (of plist types)
  • NSData, NSDate

Persistence is Automatic

Modern iOS writes changes to disk automatically. The old synchronize call is no longer needed and is deprecated.

// [defaults synchronize]; // no longer required

Registering Default Values

Provide fallback values for first launch with registerDefaults:. These apply only until the user changes them.

[defaults registerDefaults:@{@"darkMode": @NO, @"fontSize": @14}];

Removing a Value

Delete a stored key when it is no longer needed.

[defaults removeObjectForKey:@"username"];

Storing Custom Objects

To store a custom object, archive it to NSData first (it must conform to NSCoding), then save that data. NSUserDefaults cannot hold arbitrary objects directly.

Security Caveat

NSUserDefaults is not encrypted. Never store passwords, tokens, or other secrets there — use the Keychain for sensitive data.

Quick Check

Test your NSUserDefaults knowledge.

Recap

You learned NSUserDefaults persistence:

  • A simple key-value store for small preferences
  • Write with setBool:/setObject:, read with matching getters
  • Stores only property-list types
  • Persistence is automatic; synchronize is deprecated
  • Not encrypted — keep secrets in the Keychain

It is the go-to for lightweight settings.

Często zadawane pytania

Czy lekcja „Przechowywanie preferencji za pomocą NSUserDefaults” jest bezpłatna?

Tak — pełny tekst „Przechowywanie preferencji za pomocą NSUserDefaults” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Objective-C iOS Development for Legacy & Enterprise Apps, przejdź na CoddyKit PRO. Kurs Objective-C iOS Development for Legacy & Enterprise Apps zawiera 4 lekcji w sumie.

Co nauczysz się w „Przechowywanie preferencji za pomocą NSUserDefaults”?

Trwale przechowuj lekkie ustawienia i preferencje użytkowników w aplikacjach Objective-C za pomocą NSUserDefaults, najprostszego magazynu klucz-wartość w iOS. Ćwiczysz Objective-C iOS Development for Legacy & Enterprise Apps z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Objective-C iOS Development for Legacy & Enterprise Apps?

Nie wymagamy żadnego doświadczenia. Objective-C iOS Development for Legacy & Enterprise Apps w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.

Ile czasu zajmuje lekcja „Przechowywanie preferencji za pomocą NSUserDefaults”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Objective-C iOS Development for Legacy & Enterprise Apps?

Tak. Każda lekcja Objective-C iOS Development for Legacy & Enterprise Apps zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Listy właściwości i archiwizowanie
  2. Podstawy Core Data
  3. Integracja SQLite z FMDB
  4. Przechowywanie preferencji za pomocą NSUserDefaults
← Powrót do Objective-C iOS Development for Legacy & Enterprise Apps