我使用的是第三方框架,它提供了一个实例只有属性的类.在我的应用程序中,我想为这个实例添加一个额外的属性.适用于这种情况的适当方法是什么?
a)在我的应用程序中扩展框架的类
b)创建框架类的子类并定义我需要的新属性
提前致谢
以下是处理类扩展的代码段.我要做的是生成一个称为内部ID的随机ID(稍后由程序使用),它以加密形式存储在内存中.代码无法使用gcc和clang编译(我在Windows上通过GNUStep运行Objective C),每个编译器都有不同的错误消息,在代码中作为注释提到.
请注意,我知道这个问题可以通过忽略主@faceface(即#import语句之后的那个)本身的扩展和声明方法和属性来轻松解决.我使用扩展的唯一原因是这个类是由一些其他子类继承的,"internalID"属性必须是不可访问的.
#import <Foundation/Foundation.h>
@interface Foo : NSObject
{
NSString * idStr;
}
- (void)setInternalID;
- (NSString *)fetchExternalID;
@end
// Extension declaration
@interface Foo()
{ // Compilation via gcc throws error at this line stating "Expected identifier or '(' before '{' token"
NSString *internalID; // Compilation via clang throws error at this line stating "instance variables may not be placed in class extension"
}
@end
@implementation Foo
- (void)setInternalID{
internalID = [NSString stringWithFormat:
@"__KEY__INTERNAL__DUMP2872167841398551___8765%d98KLPYFGF(&^$#ESFK___JNHGV",arc4random()%100];
}
- (NSString *)fetchExternalID{
NSString …Run Code Online (Sandbox Code Playgroud) 有没有无论如何反正一些静态方法添加到类型,如Date,String,Array,等?
例如,我想将方法添加today到Date类中,在JavaScript中我可以简单地向其添加属性,或者我可以使用Object.defineProperty:
Date.today = function(){
let date = new Date;
date.setHours(0,0,0,0);
return date;
}
Run Code Online (Sandbox Code Playgroud)
Object.defineProperty(Date, 'today', { get() { ... }});
Run Code Online (Sandbox Code Playgroud)
但是我没有找到通知TypeScript这个新的静态成员.我错过了什么或者我是否以错误的方式谷歌?
我正在写一个图书馆,可能会被那些不是我的人使用.
假设我写了一堂课:
InterestingClass.h
@interface InterestingClass: NSObject
- (id)initWithIdentifier:(NSString *)Identifier;
@end
Run Code Online (Sandbox Code Playgroud)
InterestingClass.m
@interface InterestingClass()
- (void)interestingMethod;
@end
@implementation InterestingClass
- (id)initWithIdentifier:(NSString *)Identifier {
self = [super init];
if (self) {
[self interestingMethod];
}
return self;
}
- (void)interestingMethod {
//do some interesting stuff
}
@end
Run Code Online (Sandbox Code Playgroud)
如果某人稍后使用该库并决定创建一个子类InterestingClass?
InterestingSubClass.h
@interface InterestingSubClass: InterestingClass
@end
Run Code Online (Sandbox Code Playgroud)
InterestingSubClass.m
@interface InterestingSubClass()
- (void)interestingMethod;
@end
@implementation InterestingSubClass
- (void)interestingMethod {
//do some equally interesting, but completely unrelated stuff
}
@end
Run Code Online (Sandbox Code Playgroud)
未来的库用户可以从公共接口看到这initWithIdentifier是超类的方法.如果他们覆盖此方法,他们可能会(正确地)假设superclass应该在子类实现中调用该方法.
但是,如果他们定义了一个方法(在子类私有接口中),该方法无意中与超类"私有"接口中的无关方法同名?如果没有它们读取超类私有接口,他们就不会知道它们不仅仅是创建一个新方法,而且还覆盖了超类中的某些东西.子类实现可能最终被意外调用,并且在调用方法时超类期望完成的工作将无法完成.
我读过的所有SO问题似乎都暗示这就是ObjC的工作方式,并且没有办法绕过它.是这种情况,还是 …
我一直在关注Apple的MVCNetworking示例项目,并且部分接口定义令AppDelegate我感到困惑.在.h文件中我们有:
@interface AppDelegate : NSObject
{
...
Run Code Online (Sandbox Code Playgroud)
但是在.m文件中我们有这个:
@interface AppDelegate () <SetupViewControllerDelegate>
...
Run Code Online (Sandbox Code Playgroud)
所以这个类是私有地符合协议.但是你为什么要这样做而不是在标题中公开声明呢?
对不起我的英语,让我的心说话:)在我工作的一个项目中,我注意到了一个有趣的时刻.
在*.h文件中声明的接口:
@interface FrontViewController : UIViewController
...
@end
Run Code Online (Sandbox Code Playgroud)
在*.m文件中我找到了另一个界面.
@interface FrontViewController()
// Private Properties:
@property (retain, nonatomic) UIPanGestureRecognizer *navigationBarPanGestureRecognizer;
// Private Methods:
- (IBAction)pushExample:(id)sender;
@end
@implementation FrontViewController
...
@end
Run Code Online (Sandbox Code Playgroud)
为什么需要?那有什么意义呢? - 我认为这是为了方便.是?
我从迁移的Xcode 3我的旧iPhone应用程序来的Xcode 4.我收到这个代码错误,用来在Xcode中创建3 - 其实我写这样的目的,隐藏其他模块的实现细节.但是,Objective-C似乎发生了一些变化.此代码现在收到错误
无法在@interface或@protocol中声明变量.
请记住,这是.m文件顶部的代码而不是.h
@interface VisualViewController ()
BOOL doReloadPhoto;
+ (void)buildVisualEffectInfo;
@property (nonatomic, retain) HandleCheckListSetting *checkListHandler;
@end
Run Code Online (Sandbox Code Playgroud) compiler-errors objective-c instance-variables xcode4 class-extensions
我正在尝试在Array中添加扩展方法,如下所示:
extension Array {
func contains(obj: T) -> Bool {
let filtered = self.filter {$0 == obj}
return filtered.count > 0
}
}
Run Code Online (Sandbox Code Playgroud)
但是self.filter {$0 == obj}不要工作.编译错误:
无法找到接受提供的参数的'=='的重载
请参阅此类似问题:需要在User.Identity中访问更多用户属性
我想创建自定义身份验证方法以与我的Razor视图一起使用,它允许轻松访问IdentityUser与User.Identity对象关系的属性,但我不确定如何去做.我想创建一个类似于几个自定义扩展User.Identity.GetUserName(),User.Identity.GetUserById()等等...而不是使用此ViewContextExtension方法.我的身份验证类型当前是DefaultAuthenticationTypes.ApplicationCookieVS2013 MVC5模板的默认类型.正如Shoe所说,我需要在用户登录后插入此声明.
我的问题是:
如何以及在何处创建具有this IIdentityIPrincipal下的out参数的自定义声明?
这将允许我通过CookieAuthentication在DDD设置中的实体的View中访问用户属性,其中我在使用Identity 2.0的单个应用程序中有多个DbContexts.我最终将使用WebAPI,但是现在我希望尽可能简单.我找到了这个SO Q&A但是它适用于使用Tickets的Web Forms.不确定门票和代币之间的区别吗?
这是ViewContext从基本控制器使用的当前方法:
视图:
@using Microsoft.AspNet.Identity
@using Globals.Helpers
@using Identity //custom Identity for Domain
@using Microsoft.AspNet.Identity.Owin
@if (Request.IsAuthenticated)
{
var url = @ViewContext.BaseController().GetAvatarUrlById(User.Identity.GetUserId<int>());
//...
}
Run Code Online (Sandbox Code Playgroud)
BaseController.cs
public string GetAvatarUrlById(int id)
{
var user = UserManager.FindById(id);
return "../../" + user.ImageUrl;
}
Run Code Online (Sandbox Code Playgroud)
Extensions.cs
public static class ViewContextExtension
{
public static BaseController BaseController(this ViewContext view)
{
var baseController …Run Code Online (Sandbox Code Playgroud) c# class-extensions asp.net-mvc-5 razor-3 asp.net-identity-2
我一直在尝试重构代码,感谢函数并将它们添加到一个单独的文件extension中ViewController
我在这个扩展中有一个函数,它添加gesture recognizers了一些视图,这些视图引用了我放在其他文件扩展名中的函数ViewController.在构建时我收到此错误"Swift键路径中的预期表达式"
导致此错误的原因是什么?
class-extensions ×10
objective-c ×5
ios ×4
swift ×3
cocoa-touch ×2
c# ×1
gcc ×1
gnustep ×1
llvm-clang ×1
properties ×1
protocols ×1
razor-3 ×1
refactoring ×1
subclass ×1
typescript ×1
xcode4 ×1