• Home
  • Basics
  • DSA
  • MAD
  • Concept
  • Practice
  • Misc
  • Tips
  • QA’s
  • Home
  • Basics
  • DSA
  • MAD
  • Concept
  • Practice
  • Misc
  • Tips
  • QA’s
  • #News
  • #APPS
  • #Events
    • #WWDC
    • #I/O
    • #Ignite
  • #Let’s Talk
  • #Interview
  • #Tips

MyCodeTips mycodetips-newlogocopy1

  • Home
  • Basics
  • DSA
  • MAD
  • Concept
  • Practice
  • Misc
  • Tips
  • QA’s
IOS

Tips for Converting UIColor object to NSData

Tips for Converting UIColor object to NSData

Converting a UIColor object, view.backgroundColor, to an NSData object

/* 1. Archive into NSData object */

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:view.backgroundColor];

[dict setObject:data forKey:@”backgroundColor”];

…

/* 2. Unarchive from NSData object */

NSData *data = [dict objectForKey:@”backgroundColor”];

if ( data != nil ) {

view.backgroundColor = [NSKeyedUnarchiver unarchiveObjectWithData:data];

}

Code example 2: Writing an NSMutableDictionary object to a file with the converted color data stored in the dictionary

/* 1. Dictionary object and file name */

NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];

NSString *dictionaryFileName = @”…”;

/* 2. Do some dictionary stuff: adding key/object pairs */

/* … */

/* 3. UIColor object that we want to save to the file */

UIColor *myColor = [UIColor greenColor];

/* 4. Convert the UIColor object to NSData object and then add it to dictionary */

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:myColor];

[dictionary setObject:data forKey:@”myColor”];

/* 5. Write the dictionary to a file */

if ( [dictionary writeToFile:dictionaryFileName atomically:YES] == NO ) {

/* Use of NSData object instead of UIColor will avoid flowing through here */

/* ERROR: Couldn’t write to the file */

}

[dictionary release];

  • Click to share on Reddit (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • More
  • Click to share on Pocket (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
Written by Admin Blogger - March 10, 2014 - 3250 Views
Tags | IOS, UICOLOR
AUTHOR
Admin Blogger

This website is basically about of what we learnt from my years of experience as a software engineer on software development specifically on mobile application development, design patterns/architectures and its changing scenarios, security, troubleshooting, tools, tips&tricks and many more.

You Might Also Like

if-else-if-else

Conditionals in Swift ( If, Else If, Else )

August 5, 2021
objective-c-functions-methods

Functions and Methods in Objective-C

October 15, 2021
control-system-swift

Control Flow in Swift

November 18, 2021
Next Post
Previous Post

Support us

Subscribe for updates

Join 8,276 other subscribers

Latest Posts

  • primitive-datatypes-new
    Exploring the Pros and Cons of Primitive Data Types
  • best practice clean code
    Essential Coding Standards and Best Practices for Clean Code
  • YT-Featured-Templates--lld
    What Business Problems Solves in Low Level Design (LLD)
  • SRP-SingleResopnsibility
    SRP : Single Responsibility Principle in Swift and Objective-C
  • Designing Mobile APPS
    Limitation and restriction of designing mobile apps
whiteboard

Whiteboard(PRO)

whiteboard

Whiteboard(lite)

alphabets

Kids Alphabet

do2day

Do2Day

  • #about
  • #myapps
  • #contact
  • #privacy
  • #Advertise
  • #myQuestions

.Net Android Blogging Cloud Concept Database DSA ERROR ExcelTips Flutter Interview IOS IOSQuestions JAVA Javascript MAC-OS No-Mouse Objective-c Programming Quick Tips SEO Software Design & Architecture Swift SwiftUI Tips&Tricks Tools Troubleshoot Videos Web Wordpress Xamarin XCode

  • Exploring the Pros and Cons of Primitive Data Types
  • Essential Coding Standards and Best Practices for Clean Code
  • What Business Problems Solves in Low Level Design (LLD)
  • SRP : Single Responsibility Principle in Swift and Objective-C
  • Limitation and restriction of designing mobile apps
MyCodeTips

©mycodetips.com