针对objective-c–RestKitRKObjectMapping嵌套json和controller嵌套json对象这两个问题,本篇文章进行了详细的解答,同时本文还将给你拓展angular–无法
针对objective-c – RestKit RKObjectMapping嵌套json和controller嵌套json对象这两个问题,本篇文章进行了详细的解答,同时本文还将给你拓展angular – 无法解析AuthenticationService的所有参数:([object Object],?,[object Object])、Archiving Objective-C Objects with NSCoding、ASP.NET list等相关知识,希望可以帮助到你。
本文目录一览:- objective-c – RestKit RKObjectMapping嵌套json(controller嵌套json对象)
- angular – 无法解析AuthenticationService的所有参数:([object Object],?,[object Object])
- Archiving Objective-C Objects with NSCoding
- ASP.NET list
- Flutter Web Firestore 错误:应为“((Object?) => Object)?”类型的值,但得到“(Object) => Object?”类型的值?
objective-c – RestKit RKObjectMapping嵌套json(controller嵌套json对象)
{ "sucess": true,"error": { "code": "","message": "" },"data": [ { "id": 1,"name": "Salad","description": "Salad Desc","category_id": 1 },{ "id": 2,"name": "Soup","description": "Soup Desc",{ "id": 3,"name": "Wrap","description": "Wrap Desc",{ "id": 4,"name": "Appetizers","description": "Appetizers Desc","category_id": 1 } ] }
类
@interface Subcategory : NSObject @property (nonatomic,retain) NSNumber *subcategoryID; @property (nonatomic,retain) NSNumber *categoryID; @property (nonatomic,retain) Nsstring *name; @property (nonatomic,retain) Nsstring *description; @property (nonatomic,retain) Nsstring *thumbnail; @property (nonatomic,retain) NSArray *data;
视图控制器
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[Subcategory class]]; RKObjectMapping* dataMapping = [RKObjectMapping mappingForClass:[Subcategory class]]; [dataMapping mapKeyPath:@"id" toAttribute:@"subcategoryID"]; [dataMapping mapKeyPath:@"category_id" toAttribute:@"categoryID"]; [dataMapping mapAttributes:@"name",@"description",nil]; [mapping mapKeyPath:@"data" toRelationship:@"data" withMapping:dataMapping]; [[RKObjectManager sharedManager] loadobjectsAtResourcePath:[Nsstring stringWith
解决方法
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[Subcategory class]]; RKObjectMapping* dataMapping = [RKObjectMapping mappingForClass:[Subcategory class]]; [dataMapping mapKeyPath:@"id" toAttribute:@"subcategoryID"]; [dataMapping mapKeyPath:@"category_id" toAttribute:@"categoryID"]; [dataMapping mapAttributes:@"name",nil]; dataMapping.rootKeyPath = @"data"; //this is what you were missing //get your mapping provider (i'm not sure if you already have on declared) [mappingProvider setobjectMapping:dataMapping forResourcePathPattern:@"/somepath/toservice/"]; [[RKObjectManager sharedManager] loadobjectsAtResourcePath:@"/somepath/toservice/"];
以下是有关获取其他元素的问题的更多信息(例如“错误”):
- (void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError *)error { NSError *parseError = nil; id response = [objectLoader.response parsedBody:&parseError]; DLog(@"response %@",response); if (!parseError) { Nsstring *errorMessage = [[response valueForKey:@"error"] valueForKey:@"message"]; //do something with it } }
angular – 无法解析AuthenticationService的所有参数:([object Object],?,[object Object])
Can’t resolve all parameters for AuthenticationService: ([object Object],?,[object Object])
我已经检查了几乎每个主题,并尝试了多种方法来解决它,但仍然无法在第二天击败它.
我试图像这样在appService中注入第一个authService但是得到了同样的错误
@Inject(forwardRef(() => AuthenticationService)) public authService: AuthenticationService
我检查了所有DI和服务内部的导入顺序,在我看来一切都是正确的
如果有人可以帮我处理它,我很感激.
Angular 4.0.0
AuthService
import { Injectable } from '@angular/core'; import {Http,Headers,Response} from '@angular/http'; import 'rxjs/add/operator/toPromise'; import {Observable} from 'rxjs/Rx'; import {AppServices} from "../../app.services"; import {Router} from "@angular/router"; @Injectable() export class AuthenticationService { public token: any; constructor( private http: Http,private appService: AppServices,private router: Router ) { this.token = localStorage.getItem('token'); } login(username: string,password: string): Observable<boolean> { let headers = new Headers(); let body = null; headers.append("Authorization",("Basic " + btoa(username + ':' + password))); return this.http.post(this.appService.api + '/login',body,{headers: headers}) .map((response: Response) => { let token = response.json() && response.json().token; if (token) { this.token = token; localStorage.setItem('Conform_token',token); return true; } else { return false; } }); } logout(): void { this.token = null; localStorage.removeItem('Conform_token'); this.router.navigate(['/login']); } }
应用服务
import {Injectable} from '@angular/core'; import {Headers,Http,RequestOptions} from '@angular/http'; import {Router} from "@angular/router"; import {AuthenticationService} from "./auth/auth.service"; import 'rxjs/add/operator/toPromise'; import {Observable} from 'rxjs/Rx'; @Injectable() export class AppServices { api = '//endpoint/'; public options: any; constructor( private http: Http,private router: Router,public authService: AuthenticationService // doesn't work // @Inject(forwardRef(() => AuthenticationService)) public authService: AuthenticationService // doesn't work either ) { let head = new Headers({ 'Authorization': 'Bearer ' + this.authService.token,"Content-Type": "application/json; charset=utf8" }); this.options = new RequestOptions({headers: head}); } // ==================== // data services // ==================== getData(): Promise<any> { return this.http .get(this.api + "/data",this.options) .toPromise() .then(response => response.json() as Array<Object>) .catch((err)=>{this.handleError(err);}) }
应用模块
import { browserModule } from '@angular/platform-browser'; import { browserAnimationsModule } from '@angular/platform-browser/animations'; import { NgModule } from '@angular/core'; import { FormsModule } from '@angular/forms'; import {BaseRequestOptions,HttpModule} from '@angular/http'; import { MaterialModule} from '@angular/material'; import {FlexLayoutModule} from "@angular/flex-layout"; import 'hammerjs'; import { routing,appRoutingProviders } from './app.routing'; import { AppServices } from './app.services'; import {AuthGuard} from "./auth/auth.guard"; import {AuthenticationService} from "./auth/auth.service"; import {AppComponent} from './app.component'; import {AuthComponent} from './auth/auth.component'; import {NotFoundComponent} from './404/not-found.component'; import { HomeComponent } from './home/home.component'; @NgModule({ declarations: [ AppComponent,AuthComponent,NotFoundComponent,HomeComponent ],imports: [ browserModule,browserAnimationsModule,FormsModule,HttpModule,routing,MaterialModule,FlexLayoutModule ],providers: [AppServices,AuthGuard,AuthenticationService],bootstrap: [AppComponent] }) export class AppModule { }
解决方法
你可以使用
export class AuthenticationService { public token: any; appService: AppServices; constructor( private http: Http,// private appService: AppServices,injector:Injector; private router: Router ) { setTimeout(() => this.appService = injector.get(AppServices)); this.token = localStorage.getItem('token'); }
另见DI with cyclic dependency with custom HTTP and ConfigService
要避免使用setTimeout,您还可以从AppService的构造函数中设置AuthenticationService.appService(或者相反)
Archiving Objective-C Objects with NSCoding
For the seasoned Cocoa developer,this is a piece of cake. For newer developers,this can be a real pain,especially if you don't kNow what you're looking for. I get this question a decent amount,so I figured I'd put a quick guide together.
The Problem
You can't put just any object in a plist. This mainly gets people when they want to put something into NSUserDefaults and get an error (because NSUserDefaults archives to a plist under the hood).
Plists only support the core types: Nsstring
, NSNumber
,monospace">NSDate,monospace">NSData,monospace">NSArray,NSDictionary
(and their CF buddies thanks to the toll-free bridge). The key here isNSData
. You can convert any object to NSData
with the NSCoding
protocol.
The Solution
There are two things you have to do: implement NSCoding and then use the archiver and unarchiver.
Implementing NSCoding
Say you have an object that looks like this:
@interface Note : NSObject { Nsstring *title; Nsstring *author; BOOL published; } @property (nonatomic, copy) Nsstring *title; @property (nonatomic, copy) Nsstring *author; @property (nonatomic) BOOL published; @end
#import "Note.h" @implementation Note @synthesize title; @synthesize author; @synthesize published; - (void)dealloc { [title release]; [author release]; [super dealloc]; } @end
Pretty simple,right?
Now,all you have to do to implement NSCoding is the following:
NSObject <NSCoding> { Nsstring *title; Nsstring *author; dealloc { [title release]; [author release]; [super dealloc]; } - (id)initWithCoder:(NSCoder *)decoder { if (self = [super init]) { self.title = [decoder decodeObjectForKey:@"title"]; self.author = [decoder decodeObjectForKey:@"author"]; self.published = [decoder decodeBoolForKey:@"published"]; } return self; } - (encodeWithCoder:(NSCoder *)encoder { [encoder encodeObject:title forKey:@"time"]; [encoder encodeObject:author forKey:@"author"]; [encoder encodeBool:published forKey:@"published"]; } @end
Pretty simple. All I did was add the <NSCoding>
protocol delectation in the header file and initWithCoder:
and encodeWithCoder:
in the implementation. You use these methods to tell NSCoder
how to encode your object into data. Notice how two of the variables are objects and one was a BOOL
. You have to use different methods for non-objects. The NSCoder documentation has the full list.
Remember,that you can use NSCoder
to archive your object however whatever you want. It doesn't have to just be all of the instance variables in your object,although that's what you'll do 90% of the time.
Using the Archiver and Unarchiver
This part is also really easy. Let's say you have an array of notes that you want to put into NSUserDefaults
,here's the code:
// Given `notes` contains an array of Note objects NSData *data = [NSKeyedArchiver archivedDataWithRootObject:notes]; [[NSUserDefaults standardUserDefaults] setobject:data forKey:@"notes"];
Unarchiving is just as easy:
NSData *notesData = [[NSUserDefaults standardUserDefaults] objectForKey:@"notes"]; NSArray *notes = [NSKeyedUnarchiver unarchiveObjectWithData:notesData];
ASP.NET list
public partial class 测试 : System.Web.UI.Page
{
static List<Item> allAnswer= new List<Item>();
protected void Page_Load(object sender, EventArgs e)
{
//首次加载
if (IsPostBack == false)
{
//不能使用将allAnswer中的元素全部删除,这样也会将session中的值清空
//allAnswer.clean();
//使用重新定义新的空的对象来实现对allAnswer的清空
allAnswer = new List<Item>();
List<Item> reallAnswer = null;
try
{
//其中Session["ReAllAnswer"]来自于另一页面
reallAnswer = (List<Item>)Session["ReAllAnswer"];
//PrintAllAnwser(reallAnswer);
}
catch { }
}
}
如果使用allAnswer.clean()函数,则接收的数据Session["ReAllAnswer"]将会设置为空;
而使用new List<Item>(),则不会。
Flutter Web Firestore 错误:应为“((Object?) => Object)?”类型的值,但得到“(Object) => Object?”类型的值?
我认为这是 Firebase 中的一个错误。在他们修复它之前,您可以尝试从以下位置更改 firebase_core/firebase_core_web/lib/src/interop/utils/utils.dart 的本地副本:
dynamic dartify(
Object? jsObject,[
Object Function(Object? object)? customDartify,])
到
dynamic dartify(
Object? jsObject,[
Object? Function(Object object)? customDartify,])
您还需要从 cloud_firestore/cloud_firestore_web/lib/src/interop/utils/utils.dart 中删除 as Object Function(Object?)?)
。
至少这为我解决了这个问题(目前)。
,谢谢你,John Michael Gelilio。将 firebase-fire.js 版本从 8.2.9 降级到 8.0.1 后,我可以在没有空安全的情况下运行应用程序。
<script src="https://www.gstatic.com/firebasejs/8.0.1/firebase-firestore.js"></script>
以空安全运行时出现同样的错误,但目前不是问题
我们今天的关于objective-c – RestKit RKObjectMapping嵌套json和controller嵌套json对象的分享就到这里,谢谢您的阅读,如果想了解更多关于angular – 无法解析AuthenticationService的所有参数:([object Object],?,[object Object])、Archiving Objective-C Objects with NSCoding、ASP.NET list的相关信息,可以在本站进行搜索。
本文标签: