我可以做以下任何一项吗?他们会正确锁定/解锁同一个物体吗?为什么或者为什么不?假设有许多相同的线程使用全局变量"obj",它在所有线程启动之前被初始化.
1.
@synchronized(obj) {
[obj release];
obj = nil;
}
Run Code Online (Sandbox Code Playgroud)
2.
@synchronized(obj) {
obj = [[NSObject new] autorelease];
}
Run Code Online (Sandbox Code Playgroud) 我在我的项目中使用WKWebView来实现基于Web的授权UI.我使用[NSHTTPCookieStorage sharedHTTPCookieStorage]来保留整个应用程序的用户会话cookie,并在WKWebView重定向到我们的后端页面时保持用户身份验证.
问题是,在这种情况下,看起来WKWebView会忽略其他域的"Set-Cookie"标头.例如:
设置身份验证过程的初始请求:
GET /api2/providers/misfit/start/ HTTP/1.1
Host: api.welltory.com
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip, deflate
Connection: keep-alive
Proxy-Connection: keep-alive
Cookie: csrftoken=haF3PX9l6VB9DrTJTNEQvsjsAiMZTYNC;sessionid=txo4fhez18vl6mvjuwlelph1uyn1pkau
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/601.6.17 (KHTML, like Gecko) Version/9.1.1 Safari/601.6.17
X-CSRFToken: haF3PX9l6VB9DrTJTNEQvsjsAiMZTYNC
Referer: https://api.welltory.com/api2/api/version/
Accept-Language: ru
Run Code Online (Sandbox Code Playgroud)
此请求的结果是重定向到目标服务登录页面,我们在其中列出了以下请求:
GET /auth/dialog/authorize?scope=public+birthday+email+tracking+session+sleeps&state=NdhVfDpcCTfjCkuEbgnoj0E4s8pnw6wv&client_id=ZkwJzk9QvaEkzL4M&response_type=code&redirect_uri=https%3A%2F%2Fapi.welltory.com%2Fapi2%2Fproviders%2Fmisfit%2Ffinish%2F%3Fredirect_state%3DNdhVfDpcCTfjCkuEbgnoj0E4s8pnw6wv HTTP/1.1
Host: api.misfitwearables.com
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
X-CSRFToken: haF3PX9l6VB9DrTJTNEQvsjsAiMZTYNC
Connection: keep-alive
Proxy-Connection: keep-alive
Cookie: csrftoken=haF3PX9l6VB9DrTJTNEQvsjsAiMZTYNC;sessionid=txo4fhez18vl6mvjuwlelph1uyn1pkau
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/601.6.17 (KHTML, like Gecko) Version/9.1.1 Safari/601.6.17
Accept-Language: ru
Referer: https://api.welltory.com/api2/api/version/
Accept-Encoding: gzip, deflate
HTTP/1.1 302 Moved …Run Code Online (Sandbox Code Playgroud) 我已经IBDesignables在接口视图中创建了可重用的类来渲染xib.
@interface ReusableView ()
@property (nonatomic, strong) UIView *contentView;
@end
@implementation ReusableView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
[self setupXib];
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
[self setupXib];
return self;
}
- (void)setupXib {
self.contentView = [self loadFromNib];
self.contentView.frame = self.bounds;
self.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self addSubview:self.contentView];
}
- (UIView *)loadFromNib {
NSBundle *bundle = [NSBundle bundleForClass: [self class]];
UINib *nib = [UINib nibWithNibName:NSStringFromClass([self class]) bundle:bundle];
UIView *view = (UIView *)[[nib …Run Code Online (Sandbox Code Playgroud) 我在ARMv7 IOS设备上使用标准方法调配,它对我来说非常完美.
但是当我编译arm64的代码时 - 它无法从新方法调用原始方法
我调配的主要目的 - 在我的应用程序的内部方法中使用参数在另一种方法中.
我有原始的方法-(void)insertdata:(id)text,我想改变它,-(void)patchedCall:(id)text并在新方法中调用原始方法.
码:
static IMP sOriginalImp = NULL;
@interface TextOverrides: NSObject
+(void)load;
-(void)patchedinsert:(id)text;
@end
@implementation TextOverrides
+(void)load
{
//Get Implementation of original method
Class originalClass = NSClassFromString(@"DataViewController");
Method originalMeth = class_getInstanceMethod(originalClass, @selector(insertdata:));
//Save original method implementation
sOriginalImp = method_getImplementation(originalMeth);
// Get implementation of replacement method
Method replacementMeth = class_getInstanceMethod(NSClassFromString(@"TextOverrides"), @selector(patchedCall:));
//Replace methods
method_exchangeImplementations(originalMeth, replacementMeth);
}
-(void)patchedCall:(id)text
{
@synchronized(self){
//Call of original method that we save
sOriginalImp(self, @selector(insertdata:), text);
//Make …Run Code Online (Sandbox Code Playgroud) 我知道很多方法需要调用它的超类方法,有些方法不需要,
我正在寻找关于方法混合的方法.它在load方法中初始化,在教程中没有[super load].
我想知道它是错还是没有必要打电话[super load].
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];
// When swizzling a class method, use the following:
// Class class = object_getClass((id)self);
SEL originalSelector = @selector(pushViewController:animated:);
SEL swizzledSelector = @selector(flbs_pushViewController:animated:);
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
BOOL didAddMethod =
class_addMethod(class,
originalSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod));
if (didAddMethod) {
class_replaceMethod(class,
swizzledSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
});
}
#pragma mark - Method …Run Code Online (Sandbox Code Playgroud) 例如,让我们考虑ARC下面的代码:
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
@implementation NSDate (MyEvilHack)
+ (void)load {
Method originalMethod = class_getInstanceMethod(self, @selector(copyWithZone:));
Method newMethod = class_getInstanceMethod(self, @selector(myCopyWithZone:));
method_exchangeImplementations(originalMethod, newMethod);
}
- (id)myCopyWithZone:(NSZone *)zone {
id result = [self myCopyWithZone:zone];
// do customization
return result;
}
@end
Run Code Online (Sandbox Code Playgroud)
在此代码中,原始copyWithZone:方法隐式返回一个保留对象,因为它属于copy方法族.但我myCopyWithZone:的不是.
我希望崩溃,但看起来这个代码正常工作.当然,我可以重命名我的方法以避免混淆.但我很好奇在引擎盖下究竟发生了什么?
自10.12 Sierra主要更新以来,我无法仅在设备上编译我的一个iOS项目.但非常奇怪,它在模拟器上工作.我试图在Xcode中删除并重置我的开发帐户,硬重启,删除并重置钥匙串中的所有证书等.XCode 8.0.我也尝试过8.1 beta,同样的.
错误 :
SecKey API返回:-25304,(null)/ Users/****/Library/Developer/Xcode/DerivedData/**** - epkppprfmidyatftsvnxgjqsawit/Build/Products/Debug-iphoneos/****.app/Frameworks/libswiftAVFoundation.dylib:未知错误-1 = ffffffffffffffff错误:任务失败,退出1信号0 {/ usr/bin/codesign' - force'' - 签名''C47B52FDE2CABFC81D33BED8937984AF8BC6DC33'' - verbose''/ Users/****/库/开发商/ Xcode中/ DerivedData/**** - epkppprfmidyatftsvnxgjqsawit /建设/产品/调试-的iPhoneOS/***应用程序/框架/ libswiftAVFoundation.dylib"
我试图搜索以防止我当前库中的方法Swizzle,但我或多或少发现每个文档或博客文章是关于如何实现Swizzling.关于Method swizzling,我有几个问题,我无法找到所有问题.
我知道方法调整的危险,并且已经在这里查看了相关的帖子,但是找不到相关信息来阻止它.
如果上述主题中有任何文档或博客文章,我将非常感谢您的帮助.
我需要使用自动布局在其超视图的右边界外放置一个视图.
我试图通过指定以下NSLayoutConstraint来做到这一点:
NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem:self.downloadView
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:self.contentView
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:0.0];
Run Code Online (Sandbox Code Playgroud)
self.downloadView包含的是self.contentView的子视图.通过这样做iOS抱怨有以下异常:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]: Invalid pairing of layout attributes'
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么我不能配对这两个属性,我怎样才能实现我的目标?
我该怎么打电话
print(NSDate())
Run Code Online (Sandbox Code Playgroud)
和而不是接收通常的响应,获得所述一个我有一个命名的函数getString()是一个部分extension的NSDate.
这是我的扩展:
extension NSDate {
//NSDate to String
public func getString() -> String {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss ZZZ"
dateFormatter.locale = NSLocale.currentLocale()
return dateFormatter.stringFromDate(self)
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,我不想只使用:
Run Code Online (Sandbox Code Playgroud)NSDate().getString()我想覆盖
description这个类的原始.
更新:
所以,一切看起来像唯一的选择是方法调整,如果可能的话.
谁对赏金感兴趣?
当心
我这样做只是为了个人成长并理解这个概念,而不是计划在这种情况下使用它来运送应用程序,现在甚至不确定我可以使用它的场景.
ios ×8
objective-c ×6
cocoa-touch ×2
xcode ×2
arm64 ×1
autolayout ×1
cocoa ×1
code-signing ×1
ibdesignable ×1
nsdate ×1
swift ×1
swift2 ×1
wkwebview ×1