GVKun编程网logo

快速使用非NS_ENUM Objective-C枚举(非枚举的属性不合法怎么处理)

14

如果您想了解快速使用非NS_ENUMObjective-C枚举和非枚举的属性不合法怎么处理的知识,那么本篇文章将是您的不二之选。我们将深入剖析快速使用非NS_ENUMObjective-C枚举的各个方

如果您想了解快速使用非NS_ENUM Objective-C枚举非枚举的属性不合法怎么处理的知识,那么本篇文章将是您的不二之选。我们将深入剖析快速使用非NS_ENUM Objective-C枚举的各个方面,并为您解答非枚举的属性不合法怎么处理的疑在这篇文章中,我们将为您介绍快速使用非NS_ENUM Objective-C枚举的相关知识,同时也会详细的解释非枚举的属性不合法怎么处理的运用方法,并给出实际的案例分析,希望能帮助到您!

本文目录一览:

快速使用非NS_ENUM Objective-C枚举(非枚举的属性不合法怎么处理)

快速使用非NS_ENUM Objective-C枚举(非枚举的属性不合法怎么处理)

我正在使用wahoo Fitness API,它定义了以下Objective-C枚举:

typedef enum{    /** No active connection. */    WF_SENSOR_CONNECTION_STATUS_IDLE,    /** The connection is in process of being established. */    WF_SENSOR_CONNECTION_STATUS_CONNECTING,    /** The sensor connection is established and active. */    WF_SENSOR_CONNECTION_STATUS_CONNECTED,    /** The connection was interrupted (usually occurs when fisica is disconnected). */    WF_SENSOR_CONNECTION_STATUS_INTERRUPTED,    /** The connection is in process of being disconnected. */    WF_SENSOR_CONNECTION_STATUS_DISCONNECTING,} WFSensorConnectionStatus_t;

我找不到快速使用它的方法。我首先尝试对其进行切换/操作,但未成功。我正要继续进行以下操作:

var connState : WFSensorConnectionStatus_t = WF_SENSOR_CONNECTION_STATUS_IDLE...if( connState == WF_SENSOR_CONNECTION_STATUS_IDLE){

但是它不能编译:

''WFSensorConnectionStatus_t'' is not convertible to ''NSObject''

任何解决方法?我阅读使用WFSensorConnectionStatus_t.WF_SENSOR_CONNECTION_STATUS_IDLEWF_SENSOR_CONNECTION_STATUS_IDLE.value但在xcode
beta-4中不起作用。

答案1

小编典典

如您所说,从.beta 4开始,使用.value获取基础整数的解决方法不再起作用。

不幸的是an enum不能从Objective-C转移到Swift,它必须是an NS_ENUM

我在一个需要enum从Objective-C框架中进行移植并在Swift中使用它的项目中,具有与您相同的设置。

我所做的解决方法是创建一个包含的Objective-C类别,NS_ENUM然后将值从框架enum传递到自己的NS_ENUM

将类别导入桥接头中,您应该可以像平常一样使用enum

像这样:

typedef NS_ENUM(NSUInteger, ConnectionStatus) {    ConnectionStatusIdle}- (ConnectionStatus)connectionStatus {    if [self getConnectionStatus] == WF_SENSOR_CONNECTION_STATUS_IDLE {        return ConnectionStatusIdle    }}

然后,您应该可以像这样使用它:

switch myObject.connectionStatus() {    case .Idle:        // do something        break}

angular – 无法解析AuthenticationService的所有参数:([object Object],?,[object Object])

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 { }

解决方法

AppServices和AuthenticationService之间存在循环依赖关系 – 这与Angular使用的构造函数注入无法实现.

你可以使用

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(或者相反)

ios – Swift – 使用Objective-C选择器’*’的方法’*()’与来自超类’NSObject’的’*’的getter冲突,具有相同的Objective-C选择器

ios – Swift – 使用Objective-C选择器’*’的方法’*()’与来自超类’NSObject’的’*’的getter冲突,具有相同的Objective-C选择器

我将此 xcode更新为6.3.1后收到此错误消息.

/Users/MNurdin/Documents/iOS/xxxxx/Models/Message.swift:46:10: Method 'hash()' with Objective-C selector 'hash' conflicts with getter for 'hash' from superclass 'NSObject' with the same Objective-C selector

我的代码

var hash_ : UInt

func hash() -> UInt {
        return UInt(hash_);
    }

请指教.谢谢.

解决方法

详细说明:@property(readonly)NSUInteger哈希是NSObject的Objective-C属性,这意味着为该变量创建了一个getter,即hash().

您现在尝试定义一个具有相同名称和相同参数(无)但具有不同返回类型的方法(UInt而不是NSUInteger,它将是swift中的Int.).因此,您收到给定的错误.要解决该问题,您现在有两个选择:

>将返回类型更改为Int – >这将覆盖预定义的哈希函数>选择其他方法名称或添加参数

ios – 是否有编译为Objective-C或与Objective-C二进制兼容的语言 – > Objective-C的Coffeescript

ios – 是否有编译为Objective-C或与Objective-C二进制兼容的语言 – > Objective-C的Coffeescript

我最近发现了 eero programming lanugage
我发现它的概念非常有趣.它似乎主要是语法糖,看起来很可读.

虽然它不直接编译到Objective-C,但它声称生成与Objective-C相同的二进制代码

Eero compiles down to the same binary code as Objective-C

Eero offers excellent,nearly seamless interoperability with
Objective-C,C,and C++.

我发现这种方法非常有趣,我想知道是否有类似的编程语言和项目提供与Objective-C和iOS的大腿集成.
我正在为Objective-C寻找类似Coffeescript的东西.

解决方法

虽然使用eero的主要方法是将其编译为本机代码,但它也支持源到源的转换(从eero到标准的Objective-C/C++).有关详细信息,请参阅 https://github.com/eerolanguage/eero/wiki/Translator.

它确实需要记录在一个更明显的地方……

objective-c – Cocoa NSArray / NSSet:-makeObjectsPerformSelector:与快速枚举

objective-c – Cocoa NSArray / NSSet:-makeObjectsPerformSelector:与快速枚举

我想对存储在NSSet中的几个对象执行相同的操作.

我的第一个尝试是使用快速枚举:

for (id item in mySetofObjects)
    [item action];

它的工作相当不错.然后我想到:

[mySetofObjects makeObjectsPerformSelector:@selector(action)];

现在,我不知道最好的选择是什么.据我所知,这两个解决方案是等效的.但是有一些偏爱一个解决方案的争论呢?

解决方法

我会争辩使用makeObjectsPerformSelector,因为它允许NSSet对象来处理自己的索引,循环和消息分派.编写NSSet代码的人最有可能知道实现该特定循环的最佳方式.

最糟糕的是,它们将简单地实现完全相同的循环,所有您获得的是更为清晰的代码(不需要包围循环).最好的是,他们做了一些内部优化,代码实际运行得更快.

Apple的Code Speed Performance文档中的“展开循环”部分简要介绍了该主题.

如果你关心性能,最好的做法是设置一个快速程序,在一组中的对象上执行一些选择器.有几百万次的时间,两个不同的情况之间的时间差.

我们今天的关于快速使用非NS_ENUM Objective-C枚举非枚举的属性不合法怎么处理的分享已经告一段落,感谢您的关注,如果您想了解更多关于angular – 无法解析AuthenticationService的所有参数:([object Object],?,[object Object])、ios – Swift – 使用Objective-C选择器’*’的方法’*()’与来自超类’NSObject’的’*’的getter冲突,具有相同的Objective-C选择器、ios – 是否有编译为Objective-C或与Objective-C二进制兼容的语言 – > Objective-C的Coffeescript、objective-c – Cocoa NSArray / NSSet:-makeObjectsPerformSelector:与快速枚举的相关信息,请在本站查询。

本文标签: