在Objective-C中,可以编写类似的东西:
@property(retain) UIView<Protocol1, Protocol2, ...> *myView;
Run Code Online (Sandbox Code Playgroud)
但是如何在swift中编写这段代码呢?
我已经知道如何使属性符合许多协议,但它不能使用继承:
var myView: ??? protocol<Protocol1, Protocol2, ...>
Run Code Online (Sandbox Code Playgroud)
编辑:
我使用许多UIView子类型UIImageView,UILabel或者其他类型,我需要使用一些UIView属性以及协议中定义的一些方法.在最坏的情况下,我可以UIViewProtocol使用所需的属性创建一个,但我知道在Swift中是否可以声明一个属性/变量,其类型和一些协议符合.
我是精灵套件的新手,并且在更改当前的SKSpriteNode图像方面存在问题.
我的spriteNode看起来像这样
mover = [SKSpriteNode spriteNodeWithTexture:Texture1];
[mover setScale:1.0];
[self addChild:mover];
Run Code Online (Sandbox Code Playgroud)
那么我有这种方法应该改变移动者的形象,但事实并非如此.我究竟做错了什么?
- (void)didBeginContact:(SKPhysicsContact *)contact {
if (contact.bodyA.categoryBitMask == worldCategory) {
SKTexture* explodeTexture1 = [SKTexture textureWithImageNamed:@"explode"];
explodeTexture1.filteringMode = SKTextureFilteringNearest;
mover = [SKSpriteNode spriteNodeWithTexture:explodeTexture1];
}
}
Run Code Online (Sandbox Code Playgroud) 我知道这个问题已经回答[这里](lateinit属性尚未初始化).但这是由于黄油刀库问题但我的情况有所不同.完全不同.在我的情况下,当我在我的基本活动中使用匕首注入属性时,它显示我的错误引起:kotlin.UninitializedPropertyAccessException:lateinit属性pref尚未初始化
但是当我在我的子活动(登录活动)中使用它时,它的工作正常.
例如.pref.setLanguage("abc") - >它在登录活动中工作正常,但在基本活动中引发错误
这是我的代码:
BaseActivity
abstract class BaseActivity : AppCompatActivity() {
@Inject
lateinit var pref: AppSharedPreferences
@Inject
lateinit var utils: Utils
lateinit var mCurrentLanguage: String
protected lateinit var progressBarUtils: ProgressBarUtils
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
progressBarUtils = ProgressBarUtils(this)
mCurrentLanguage = "EN"
pref.setSelectedLanguage(mCurrentLanguage)
}
}
Run Code Online (Sandbox Code Playgroud)
LoginActivity
class LoginActivity : BaseActivity() {
private val TAG = this.javaClass.name
lateinit var loginViewModel: LoginViewModel
@Inject
lateinit var viewModelFactory: LoginViewModelFactory
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
AndroidInjection.inject(this)
loginViewModel …Run Code Online (Sandbox Code Playgroud) 我来自AngularJS世界,几天前开始用react-router,在AngularJS 中编写我的第一个 React 应用程序:
app.directive('Loading', function($rootScope, $timeout) {
return {
restrict: 'E',
replace: true,
template: '<p>Loading</p>'
link: function(scope, element) {
$rootScope.$on('$routeChangeStart', function(event, currentRoute, previousRoute) {
element.removeClass('ng-hide');
});
$rootScope.$on('$routeChangeSuccess', function() {
element.addClass('ng-hide');
});
}
};
});
Run Code Online (Sandbox Code Playgroud)
然后我只需添加<Loading></Loading>. 所以现在在我的 React 应用程序中,我有:
class App extends Component {
render() {
return (
<Router>
<div>
<ul>
<li><Link to="/">Home</Link></li>
<li><Link to="/about">About</Link></li>
</ul>
<hr/>
<Route exact path="/" component={Home}/>
<Route path="/about" component={About}/>
</div>
</Router>
);
}
}
Run Code Online (Sandbox Code Playgroud)
我的两个组件很简单:
class Home …Run Code Online (Sandbox Code Playgroud) 由于未捕获的异常,我收到错误终止应用程序
'NSUnknownKeyException', reason: '[<KGM_IOS.NotEkle 0x7fcb5ee0cd40> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key notEkleTextView.'.
Run Code Online (Sandbox Code Playgroud)
我有一个名为“notEkle”的 XIB,在“notEkle”里面我有 UITextView,而 XIB 提供者是 ViewController。当我将此文本与控制器连接时,出现此错误。我将我的 XIB 加载到控制器中
let notEkleView: UIView = NotEkle().loadNib() as UIView
Run Code Online (Sandbox Code Playgroud)
我将我的文本视图与控制器连接起来
@IBOutlet weak var notEkleTextView: UITextView!
Run Code Online (Sandbox Code Playgroud)
在连接 UITextview 和控制器之前我没有任何问题。
let notEkleView: UIView = NotEkle().loadNib() as UIView
Run Code Online (Sandbox Code Playgroud)
let notEkleView: UIView = NotEkle().loadNib() as UIView
Run Code Online (Sandbox Code Playgroud) 在下面的代码中,Alamofire抱怨它"无法使用类型((_,_,_,_) - > _)的参数列表调用'responseJSON'." 使用"响应"时会出现同样的问题.
func request(parameters: [String : AnyObject?], completionHandler: ((NSURLRequest, NSHTTPURLResponse?, AnyObject?, NSError?) -> Void)) -> Void {
Alamofire.request(.POST, "localhost:8080/reserve", parameters:
["refreshToken": refreshToken,
"accessToken": accessToken,
"deviceToken": deviceToken],
encoding: .JSON)
.responseJSON { (request, response, data, error) in
completionHandler(request, response, data, error)
}
}
Run Code Online (Sandbox Code Playgroud)
这是为什么会发生的?
\n\n\n错误域= NSCocoaErrorDomain代码= 513“您\ xe2 \ x80 \ x99t无权将文件\ xe2 \ x80 \ x9csubash \ xe2 \ x80 \ x9d保存在文件夹\ xe2 \ x80 \ x9ctmp \ xe2 \ x80 \ x9d中。 ” UserInfo={NSFilePath=file:///private/var/mobile/Containers/Data/Application/902FE064-C3EC-42B5-A8F8-3D2923947067/tmp/subash,NSUnderlyingError=0x281e5c6f0 {错误域=NSPOSIXErrorDomain代码=1“操作不允许”}}
\n
do {\n var mytmppath:String=FileManager.default.temporaryDirectory.absoluteString+"subash"\n try FileManager.default.createDirectory(atPath: mytmppath, withIntermediateDirectories: true, attributes: nil)\n\n print( FileManager.default.subpaths(atPath: FileManager.default.temporaryDirectory.absoluteString))\n} catch {\n print(error)\n}\nRun Code Online (Sandbox Code Playgroud)\n swift ×4
ios ×3
alamofire ×1
android ×1
dagger-2 ×1
http ×1
javascript ×1
kotlin ×1
objective-c ×1
protocols ×1
react-redux ×1
react-router ×1
reactjs ×1
redux ×1
sprite-kit ×1