使用惰性初始化器时,是否有可能保留周期?
在博客文章和许多其他地方[unowned self]
可见
class Person {
var name: String
lazy var personalizedGreeting: String = {
[unowned self] in
return "Hello, \(self.name)!"
}()
init(name: String) {
self.name = name
}
}
Run Code Online (Sandbox Code Playgroud)
我试过这个
class Person {
var name: String
lazy var personalizedGreeting: String = {
//[unowned self] in
return "Hello, \(self.name)!"
}()
init(name: String) {
print("person init")
self.name = name
}
deinit {
print("person deinit")
}
}
Run Code Online (Sandbox Code Playgroud)
像这样使用它
//...
let person = Person(name: "name")
print(person.personalizedGreeting)
//..
Run Code Online (Sandbox Code Playgroud)
并发现"人员deinit"被记录下来.
所以似乎没有保留周期.根据我的知识,当一个块捕获自我并且当该块被自己强烈保留时,存在保留周期.这种情况看起来类似于保留周期但实际上并非如此.
memory-leaks memory-management lazy-initialization automatic-ref-counting swift
我正在学习教程中带动画的自动布局
http://weblog.invasivecode.com/post/42362079291/auto-layout-and-core-animation-auto-layout-was
事情很完美.
当我尝试在我的应用程序中使用此概念时,尝试从下到上设置屏幕(UIView)的动画,当设置屏幕只是一个空的UIView时它很有用,
但是如果我将UILabel作为子视图添加到此设置屏幕,动画就会消失.在从设置屏幕中删除此UILabel时,动画可见.
这是我连接的插座
__weak IBOutlet UIView *settingsView;
__weak IBOutlet NSLayoutConstraint *settingsBottomConstraint;
__weak IBOutlet NSLayoutConstraint *settingsViewHeightConstraint;
Run Code Online (Sandbox Code Playgroud)
View确实加载了设置方法(setupViews)
-(void)setupViews
{
settingsBottomConstraint.constant = - settingsViewHeightConstraint.constant;
[settingsView setNeedsUpdateConstraints];
[settingsView layoutIfNeeded];
isSettingsHidden = YES;
}
Run Code Online (Sandbox Code Playgroud)
隐藏/取消隐藏方法
- (IBAction)showSettingsScreen:(id)sender {
if (isSettingsHidden) {
settingsBottomConstraint.constant = 0;
[settingsView setNeedsUpdateConstraints];
[UIView animateWithDuration:.3 animations:^{
[settingsView layoutIfNeeded];
}];
}
else{
settingsBottomConstraint.constant = - settingsViewHeightConstraint.constant;
[settingsView setNeedsUpdateConstraints];
[UIView animateWithDuration:0.3 animations:^{
[settingsView layoutIfNeeded];
}];
}
isSettingsHidden = !isSettingsHidden;
}
Run Code Online (Sandbox Code Playgroud)
我的问题似乎与UIView Auto Layout Animation的问题类似
我已经检查了文档支持的官方链接,并且很清楚UIWebview中不支持docx,
https://developer.apple.com/library/ios/qa/qa1630/_index.html
但是,我也发现有些人能够在UIWebview中打开docx文件,
无法使用OpenIn功能在应用程序中通过webbrowser打开docx文件, 为什么doc和docx在iOS中丢失样式信息?, 如何在iPhone中打开Microsoft Office文档
另外,iOS Safari浏览器是基于UIWebview建立的,我可以在浏览器中从互联网上打开docx文件.但是当我从我的测试服务器下载docx时(我已经通过从模拟器导入它来交叉检查下载的docx并且它在我的mac上完全打开),我无法在UIWebview中查看它.
我很困惑,docx是否支持?或者似乎docx有更多种格式,有些格式支持?
这是我的加载代码,
NSURLRequest *request = [NSURLRequest requestWithURL:urlPath];
[webViewForDocsView loadRequest:request];
Run Code Online (Sandbox Code Playgroud) 在NSArray.h中,我看到了这样的界面
@interface NSArray<ObjectType>
Run Code Online (Sandbox Code Playgroud)
有什么意义<ObjectType>
?
我创建了这个Swift类:
@objc public class Tester: NSObject {
private var name: String
private var user: Users
init(string:String, user: Users) {
print(user.empId)
print(user.name)
self.user = user
self.name = string
super.init()
}
}
Run Code Online (Sandbox Code Playgroud)
我从Obj C调用初始化程序,如下所示:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
NSString * nilString = nil;
Users * nilUser = nil;
Tester * test = [[Tester alloc] initWithString:nilString user:nilUser];
return YES;
}
Run Code Online (Sandbox Code Playgroud)
在这里,我将nil
参数传递给Swift初始化器.理想情况下,我希望这会崩溃,因为初始化程序只接受非nil
值.
但实际发生的是,当执行点到达初始化程序内部时,会创建新的参数对象:
的nil
字符串将成为""
与User
那是可变 …
继承我的应用程序中的UIDocuemtnInteractionController(不显示邮件选项)
这是Apples示例项目使用的那个
以下是各自的代码
我的应用程序
docInteractionController = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
[docInteractionController presentOpenInMenuFromBarButtonItem:(UIBarButtonItem*)sender animated:YES];
Run Code Online (Sandbox Code Playgroud)
Apple示例项目
NSURL *fileURL;
if (cellIndexPath.section == 0)
{
// for section 0, we preview the docs built into our app
fileURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:documents[cellIndexPath.row] ofType:nil]];
}
else
{
// for secton 1, we preview the docs found in the Documents folder
fileURL = [self.documentURLs objectAtIndex:cellIndexPath.row];
}
self.docInteractionController.URL = fileURL;
[self.docInteractionController presentOptionsMenuFromRect:longPressGesture.view.frame
inView:longPressGesture.view
animated:YES];
Run Code Online (Sandbox Code Playgroud)
我应该怎样做才能获得邮件选项?
我在这里阅读 mongodb 的文档,我无法理解这两个命令以及它们之间的区别。
db.users.find( { finished: { $elemMatch: { $gt: 15, $lt: 20 } } } )
Run Code Online (Sandbox Code Playgroud)
我的理解:至少有一个元素需要同时满足这两个条件。
和
元素组合满足标准...一个元素可以满足大于 15 的条件,另一个元素可以满足小于 20 的条件,或者单个元素可以同时满足这两个条件
db.users.find( { finished: { $gt: 15, $lt: 20 } } )
Run Code Online (Sandbox Code Playgroud)
问题:数组的范围匹配是如何发生的?是否就像如果一个元素满足$gt:15
,这个条件用完,其他元素检查其余条件,即$lt:20
?
http://180.160.1.140/webapp/camera?id=fksmf84-8493-45u3
Run Code Online (Sandbox Code Playgroud)
如何获得这个网址部分fksmf84-8493-45u3
? - 这部分在运行时间不断变化.
这是我到目前为止所尝试的.
NSString *url = @"http://180.160.1.140/webapp/camera?id=";
NSArray *parts = [url componentsSeparatedByString:@"="];
NSString *personID = [parts lastObject];
NSLog(@"My ID: %@", personID);
Run Code Online (Sandbox Code Playgroud) 我想扫描一个字符串并指向一个char
指向这个扫描字符串的指针.
int main(int argc, char *argv[]) {
char *string;
scanf("%s",&string);
printf("%s\n",string);
}
Run Code Online (Sandbox Code Playgroud)
但是发出警告说
警告:格式'%s'需要类型为'char*'的参数,但参数2的类型为'char**'
如何在char *
没有警告的情况下扫描字符串.
编辑:它对下面的代码有警告,我把它深入到上面的代码,所以非常具体.
int main(int argc, char *argv[]) {
int n = 2;
char *strings[2];
for(int i = 0; i < n; i++){
scanf("%s",&strings[i]);
printf("%s\n",&strings[i]);
}
}
Run Code Online (Sandbox Code Playgroud)