Вы после опции {observe: 'response'}
для HttpClient
:
httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/x-www-form-urlencoded' }),
responseType: 'text' as 'json',
observe: 'response'
};
Теперь HttpClient.get () возвращает Observable типизированного HttpResponse, а не только данные JSON. ] blockquote>
См. Чтение полного ответа в документах.
Первый шаг - заставить ваш класс Person реализовать протокол NSCoding. Основная стратегия заключается в реализации двух методов для сериализации и несериализации каждой из переменных экземпляра объекта, которые вы хотите сохранить между сеансами.
#pragma mark NSCoding Protocol
- (void)encodeWithCoder:(NSCoder *)encoder;
{
[encoder encodeObject:[self foo] forKey:@"foo"];
[encoder encodeDouble:[self bar] forKey:@"bar"];
}
- (id)initWithCoder:(NSCoder *)decoder;
{
if ( ![super init] )
return nil;
[self setFoo:[decoder decodeObjectForKey:@"foo"]];
[self setBar:[decoder decodeDoubleForKey:@"bar"]];
return self;
}
Чтобы фактически записать объекты на диск, вы можете использовать NSArray writeToFile:
метод, или используйте класс NSKeyedUnarchiver, если вы хотите более подробно рассказать о том, как это делается. В обоих случаях вы также можете поместить свой массив в другую структуру данных (например, в словарь), если хотите включить другие элементы (например, номер формата файла) в ваш файл данных.
Mr. Charbonneau has the right idea. An NSCoder
abstracts the specific serialization of your object, and lets you worry only about what needs to be serialized/deserialized. In -encodeWithCoder:
, you might want to
NSAssert1([encoder allowsKeyedCoding],
@"%@ does not support sequential archiving.",
[self className]);
as not all coders support keyed archiving.
In -initWithCoder
, you should be sending -initWithCoder:
– not simply -init
– to super
prior to initializing your object:
self = [super initWithCoder:decoder];
if (!self) return nil;
// now use the coder to initialize your state
Alternatively, since your object is basically a property list already, you can add something like -[Person plistRepresentation]
:
- (NSDictionary *)plistRepresentation
{
return [NSDictionary dictionaryWithObjectsAndKeys:
[self firstName], @"firstName",
[self lastName], @"lastName",
[NSNumber numberWithInteger:[self age]], @"age", nil];
}
Then, to serialize an array of Person
s, you can yourself transform the persons into their plistRepresentation
and then use -[NSArray writeToURL:atomically:]
. (You can of course also employ the methods of NSProperyListSerialization
directly.)