diff --git a/plist-cil/BinaryPropertyListWriter.cs b/plist-cil/BinaryPropertyListWriter.cs index 4bfa4b6..ca7adcb 100644 --- a/plist-cil/BinaryPropertyListWriter.cs +++ b/plist-cil/BinaryPropertyListWriter.cs @@ -291,7 +291,7 @@ namespace Claunia.PropertyList { idMap.Add(obj); } - else if(!this.ReuseObjectIds && obj is NSString && !IsSerializationPrimitive((NSString)obj)) + else if (!this.ReuseObjectIds && obj is NSString && !IsSerializationPrimitive((NSString)obj)) { idMap.Add(obj); } @@ -314,9 +314,9 @@ namespace Claunia.PropertyList var first = idMap.OfType().First(v => NSObject.ArrayEquals(v.Bytes, uid.Bytes)); return idMap.IndexOf(first); } - else if (!this.ReuseObjectIds && obj is NSArray) + else if (!this.ReuseObjectIds && (obj is NSArray || (obj is NSString && !IsSerializationPrimitive((NSString)obj)))) { - int index = 0; + int index = -1; for (int i = 0; i < idMap.Count; i++) { @@ -327,6 +327,11 @@ namespace Claunia.PropertyList } } + if (index == -1) + { + throw new InvalidOperationException(); + } + return index; } else diff --git a/plist-cil/NSDictionary.cs b/plist-cil/NSDictionary.cs index 7804c85..2868268 100644 --- a/plist-cil/NSDictionary.cs +++ b/plist-cil/NSDictionary.cs @@ -45,12 +45,17 @@ namespace Claunia.PropertyList { readonly Dictionary dict; + // Maps the keys in this dictionary to their NSString equivalent. Makes sure the NSString + // object remains constant accross calls to AssignIDs and ToBinary + readonly Dictionary keys; + /// /// Creates a new empty NSDictionary. /// public NSDictionary() { dict = new Dictionary(); + keys = new Dictionary(); } /// @@ -366,7 +371,7 @@ namespace Claunia.PropertyList foreach (KeyValuePair entry in dict) { - new NSString(entry.Key).AssignIDs(outPlist); + keys[entry.Key].AssignIDs(outPlist); } foreach (KeyValuePair entry in dict) @@ -380,7 +385,7 @@ namespace Claunia.PropertyList outPlist.WriteIntHeader(0xD, dict.Count); foreach (KeyValuePair entry in dict) { - outPlist.WriteID(outPlist.GetID(new NSString(entry.Key))); + outPlist.WriteID(outPlist.GetID(keys[entry.Key])); } foreach (KeyValuePair entry in dict) { @@ -488,6 +493,7 @@ namespace Claunia.PropertyList public void Add(string key, NSObject value) { dict.Add(key, value); + keys.Add(key, new NSString(key)); } /// @@ -516,6 +522,7 @@ namespace Claunia.PropertyList /// Key. public bool Remove(string key) { + keys.Remove(key); return dict.Remove(key); } @@ -534,7 +541,7 @@ namespace Claunia.PropertyList /// Gets or sets the at the specified index. /// /// Index. - public NSObject this [string index] + public NSObject this[string index] { get { @@ -542,6 +549,11 @@ namespace Claunia.PropertyList } set { + if (!keys.ContainsKey(index)) + { + keys.Add(index, new NSString(index)); + } + dict[index] = value; } } @@ -579,6 +591,7 @@ namespace Claunia.PropertyList /// Item. public void Add(KeyValuePair item) { + keys.Add(item.Key, new NSString(item.Key)); dict.Add(item.Key, item.Value); } @@ -587,6 +600,7 @@ namespace Claunia.PropertyList /// public void Clear() { + keys.Clear(); dict.Clear(); } @@ -618,6 +632,7 @@ namespace Claunia.PropertyList /// true if successfully removed, false if not, or if item is not in current instance. public bool Remove(KeyValuePair item) { + keys.Remove(item.Key); return dict.Remove(item.Key); }