Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3346114
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:15:55+00:00 2026-05-18T01:15:55+00:00

I have been playing with this for a few hours and I’m stuck. I’m

  • 0

I have been playing with this for a few hours and I’m stuck. I’m trying to save a list of Favorite objects in the NSUserDefaults using Monotouch. I believe that I am on the right track but I just can’t quite get it… here are my model objects:

public class Favorite {
    public Favorite (){}
    public string Description {get;set;}
    public Song Song {get;set;}
}

public class Song {
    public Song (){}
    public string Name {get;set;}
    public string Artist {get;set;}
}

Next, I want to save a list of Favorites that the user has selected. From what I have read, I can use an NSArray to save a list of items in the NSUserDefaults. So how do I go from a List of Favorites to an NSArray of Favorites… I haven’t been able to find any documentation on this. Here is my Settings wrapper:

public class Settings {
    private static string _favoritesKey = "favorites";
    public static IList<Favorite> Favorites {get;set;}

    public static void Add(Favorite favorite){
        Favorites.Add(favorite);
    }

    public static void Remove(Favorite favorite){
        Favorites.Remove(favorite);
    }

    public static void Read()
    {
        var tempFavorites = (NSArray)NSUserDefaults.StandardUserDefaults[_favoritesKey];
        if(tempFavorites == null){
            Favorites = new List<Favorite>();   
        }
        else {
            for(uint i=0;i<tempFavorites.Count;i++){
                var fav = tempFavorites.ValueAt(i); //returns IntPtr
                // do something to convert to Favorite
                // Favorites.Add(converted_favorite);
            }
        }
    }

    public static void Write()
    {
        var tempArray = Favorites.ToArray();
        // convert to NSObject[]
        NSUserDefaults.StandardUserDefaults[_favoritesKey] = NSArray.FromNSObjects(converted_array);
        NSUserDefaults.StandardUserDefaults.Synchronize();
    }
}

Am I on the right track? It looks like all I need to do is figure out how to convert to and from NSObjects. Also, if I am saving these custom objects in NSUserDefaults, do they need to be serializable? Much thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T01:15:56+00:00Added an answer on May 18, 2026 at 1:15 am

    If you want to do this, you would need your Favorite class to be a NSObject with native storage that you synchronize with the [Connect] attribute, something like this:

    [Register]
    public class Favorite : NSObject {
    
      [Connect]
      public string Description {
        get {
          return (string) this.GetNativeField ("Description");
        }
        set {
          this.SetNativeField ("Description", new NSString (value));
        }
      }
    }
    

    You would do the same for your Song class. You can only store native classes in the NSStandardUserDefaults object store.

    An alternative would be what Jason suggested and just serialize to a string and then store that as a NSString.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been playing with this one for a few days now, and keep
Okay. Now I give up. I have been playing with this for hours. I
I've been playing around with this for a couple of hours and have come
I've been playing with Python and I have this list that I need worked
I have been playing around with a few ways to batch insert a list
I have been playing with setting up different relationships for a few hours now
I have been playing around with tableviews for the past few days trying to
I am working on this site: beta.us.lt And I have been playing with firebug
I have been playing with NodeJS lately and i found myself stuck with a
I have been playing around with Google Apps Script today and I am trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.