在swift中,我正在编写一个扩展(如Obj-C类),它在代码类方法中维护"房屋颜色".我想知道是否有一种方法可以使用IBInspectable或其他东西使接口构建器可以访问此颜色扩展,而不是将颜色专门附加到UIView子类,就像我在很多IBInspector样本用法中看到的那样.
extension UIColor {
// house blue
@IBInspectable var houseBlue: UIColor { return UIColor(red:(51/255), green:(205/255), blue:(255/255), alpha:(1)) }
// house gray
@IBInspectable var houseGray: UIColor { return UIColor(white:0.4, alpha: 1) }
// house white
@IBInspectable var houseWhite: UIColor { return UIColor.whiteColor() }
}
Run Code Online (Sandbox Code Playgroud) 尽可能简单地说UITapGestureRecognizer,我已经分配给一个UIImageView将不可避免地和莫名其妙地停止发射它所针对的方法.
它甚至更奇怪,因为有时它只会在一次敲击后停止识别手势,而其他时间则需要数十次敲击才能识别.但是,每次都没有失败,它将不可避免地停止.
我已经尝试将轻拍手势设置为强关联属性,但这没有任何效果.
我最近尝试过的(没有成功)是,在手势的选择器方法运行之后,我删除了手势,然后重新分配并重新初始化了一个新的UITapGestureRecognizer,没有任何效果.这让我相信问题就在于UIImageView而不是UITapGuestureRecognizer- 而是说,我不知道.
但是,我正在UIImageView通过几个UIView动画,所以也许有事情可做呢?
此外,UIImageView已启用用户交互,我从不禁用它.
有什么建议?我很乐意发布代码,如果这会有所帮助.这是一些代码:
设置UIImageView(图像视图和点击手势都已成为属性,以便我可以强烈关联它们):
self.cardImageView = [[UIImageView alloc] initWithFrame:frame];
[self.cardImageView setContentMode:UIViewContentModeScaleAspectFill];
[self.cardImageView setClipsToBounds:TRUE];
[self.cardImageView setBackgroundColor:[UIColor nearBlack]];
[self.cardImageView.layer setBorderColor:[[UIColor fiftyGray]CGColor]];
[self.cardImageView.layer setBorderWidth:1.0];
[self.cardImageView setUserInteractionEnabled:TRUE];
self.imageFullScreenTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView)];
[self.cardImageView addGestureRecognizer:self.imageFullScreenTap];
[view addSubview:self.cardImageView];
Run Code Online (Sandbox Code Playgroud)
动画:
[UIView animateWithDuration:0.2 animations:^
{
[self.cardImageView setFrame:[self frameForImageView]];
[page setAlpha:!fullscreenTemplate];
[saveExitButton setAlpha:!fullscreenTemplate];
[optionsButton setAlpha:!fullscreenTemplate];
if(fullscreenTemplate)
{
[self.cardImageView.layer setBorderColor:[UIColor clearColor].CGColor];
[self.view setBackgroundColor:[UIColor blackColor]];
}
else
{
[self.cardImageView.layer …Run Code Online (Sandbox Code Playgroud) 我写了一个包含基本资产数据的结构:
struct Asset: AssetProtocol {
init (id: Int = 0, url: String) {
self.id = id
self.url = URL(string: url)
}
var id: Int
var url: URL?
}
Run Code Online (Sandbox Code Playgroud)
它订阅 AssetProtocol
protocol AssetProtocol {
var id: Int { get }
var url: URL? { get }
}
Run Code Online (Sandbox Code Playgroud)
我希望扩展Array(Sequence),其中该Array中的元素是订阅AssetProtocol的项目.此外,这些函数能够在适当的位置改变数组.
extension Sequence where Iterator.Element: AssetProtocol {
mutating func appendUpdateExclusive(element newAsset: Asset) {
...
Run Code Online (Sandbox Code Playgroud)
我怎样才能迭代并改变这个序列?我已经尝试了六种方法,似乎无法做到正确!
这个问题可能是非常简单的,如果你有一些经验Python Flask,Boto3,Pillow(又名PIL)。
我试图从客户端(仅允许接收传入图像.jpg,.jpeg,.tif,),我想使用它上传到Amazon S3之前,读取的图像的尺寸Boto3。
该代码相当简单:
file = request.files['file']
# produces an instance of FileStorage
asset = models.Asset(file, AssetType.profile_img, donor.id)
# a model managed by the ORM
img = Image.open(BytesIO(file.stream.read()))
# produces a PIL Image object
size = img.size
# read the size of the Image object
asset.width = size[0]
asset.height = size[1]
# set the size to the ORM
response = s3.Object('my-bucket', asset.s3_key()).put(Body=file)
# …Run Code Online (Sandbox Code Playgroud) python amazon-s3 amazon-web-services python-imaging-library python-3.x
这可能是一个快速修复,但我已经陷入停顿,我希望你能帮助.请耐心等待,我在命令行环境中不会流利.
我刚刚开始使用名为Flask的Python框架.它已成功安装,我启动并运行Hello World.当我在浏览器中调用程序时,控制台向我发送了日志.
为了退出控制台日志,我按下了ctrl-z(^ Z)~~可能是错误开始的地方?~~并且提示:
[1]+ Stopped python hello.py
Run Code Online (Sandbox Code Playgroud)
现在,当我或者a)尝试在浏览器中运行程序或b)在命令行中运行脚本时,python hello.py我抛出一个错误:
socket.error: [Errno 48] Address already in use
Run Code Online (Sandbox Code Playgroud)
..当然还有许多其他线条印在控制台上.
一个好的答案应该包括我做错了什么以及我能做些什么来解决它,并且接受的答案也将包括为什么;)
Swift 3中的NotificationCenter似乎有一些更改,我似乎不太正确。
使用方法:
Apple Swift version 3.0.2 (swiftlang-800.0.63 clang-800.0.42.1)
Run Code Online (Sandbox Code Playgroud)
我有一个单例对象:
class Notifications {
private static let pipeline = Notifications()
...
Run Code Online (Sandbox Code Playgroud)
接收并排队订阅的项目NotificationsPipelineProtocol。(它们都是纯快捷方式,此处没有Objective-C NSObject。)
private func enqueueNotification(_ notification: NotificationsPipelineProtocol) {
...
Run Code Online (Sandbox Code Playgroud)
在其中将自己添加为NotificationCenter的观察者
NotificationCenter.default.addObserver(self,
selector: #selector(Notifications.didReceiveNotificationCompletion(_:)),
name: notification.completionNotificationName,
object: notification)
Run Code Online (Sandbox Code Playgroud)
注意 - notification.completionNotificationName是产生Notification.Name项目的计算变量。
但是,当NotificationsPipelineProtocol项目发布到NotificationCenter时:
NotificationCenter.default.post(name: self.completionNotificationName, object: self)
Run Code Online (Sandbox Code Playgroud)
观察者不调用它的关联预订方法:
@objc private func didReceiveNotificationCompletion(_ notification : Notification) {
...
Run Code Online (Sandbox Code Playgroud)
你知道为什么吗?有没有一种方法可以查看在NotificationCenter中查看特定项目的通知?可能是单例对象掉线了吗?也许#selector的格式不正确?
XCode没有给我任何警告或错误。
提前致谢。
图像是来自Swift游乐场的屏幕截图 - >左侧的代码,右侧的日志(如果您可以调用它).
我想我预计会发生的是第8行会导致1,因为,你知道,0 + 1 = 1
任何人都可以解释这里发生了什么?

现在用println

在你说出任何事情之前,我知道半冒号现在没用了,这是习惯,因为我今天决定学习来自Obj-C的Swift.
我的 Python Flask 应用程序使用 WTForms 和内置的 Python Enum 支持。我正在尝试提交一个表单 (POST),其中 SelectField 由枚举的所有值填充。
当我点击“提交”时,出现错误“不是有效的选择”。这看起来很奇怪,因为在检查传入表单的值时,该表单似乎确实包含提供的 Enum 值列表中的有效选择。
我正在使用名为 Enum 的子类,AJBEnum其格式如下:
class UserRole(AJBEnum):
admin = 0
recipient = 1
Run Code Online (Sandbox Code Playgroud)
我选择这样做是因为我在整个项目中使用了许多枚举,并希望编写一个帮助函数来收集所有选择并将它们格式化为 WTForm SelectField 元组友好。AJBEnum 的格式如下:
class AJBEnum(Enum):
@classmethod
def choices(cls, blank=True):
choices = []
if blank == True:
choices += [("", "")]
choices += [(choice, choice.desc()) for choice in cls]
return choices
Run Code Online (Sandbox Code Playgroud)
这意味着我可以UserRole在创建 SelectField 期间为 WTForms 提供所有选择,如下所示:
role = SelectField('Role', choices=UserRole.choices(blank=False), default=UserRole.recipient)
Run Code Online (Sandbox Code Playgroud)
请注意,函数参数blank提供了一个额外的空白 SelectField 选项,以防 SelectField 是可选的。在这种情况下,事实并非如此。 …