小编Sun*_*kas的帖子

尾随where子句用于扩展非泛型类型

我有以下代码:

func registerNotification(name:String, selector:Selector)
{
    NSNotificationCenter.defaultCenter().addObserver(self, selector: selector, name: name, object: nil)
}

func registerKeyboardNotifications()
{
    let isInPopover = navigationController?.popoverPresentationController != nil
    let ignore = isInPopover && DEVICE_IS_IPAD
    if !ignore {
        registerNotification(UIKeyboardWillShowNotification, selector: Selector("keyboardWillShow:"))
        registerNotification(UIKeyboardWillHideNotification, selector: Selector("keyboardWillHide:"))
    }
}
Run Code Online (Sandbox Code Playgroud)

在延伸UIViewController.许多viewcontroller重用此代码来注册键盘通知.但是使用Swift 2.2会产生警告.我喜欢新的#selector语法,但不知道在这种情况下如何实现它.

我认为正确的解决方案是制定协议并UIViewController仅针对符合该协议的实例进行扩展.我的代码到目前为止:

@objc protocol KeyboardNotificationDelegate
{
    func keyboardWillShow(notification: NSNotification)
    func keyboardWillHide(notification: NSNotification)
}

extension UIViewController where Self: KeyboardNotificationDelegate
{
    func registerKeyboardNotifications()
    {
        let isInPopover = navigationController?.popoverPresentationController != nil
        let ignore = isInPopover && DEVICE_IS_IPAD …
Run Code Online (Sandbox Code Playgroud)

ios swift swift2 protocol-extension swift2.2

32
推荐指数
2
解决办法
7331
查看次数

防止NSURLSession缓存响应

为什么它会缓存响应.它返回先前获取的响应.它甚至可以在关闭网络连接时工作.重置iOS模拟器似乎也不起作用.提出请求,然后再次使用Internet脱机工作(缓存).

public let urlSession: NSURLSession

public init()
{
    // ...

    var configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
    configuration.requestCachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData
    urlSession = NSURLSession(configuration: configuration)
}

func makeRequest(path: String, httpMethod:String, body:String?, baseUrl: String?, headers:[String:String]=[:], callback: JsonRequestCallback)
{
    let urlString = (baseUrl ?? customOAuth2Manager.API_URL) + path
    let url = NSURL(string: urlString)
    let request = oauthInstance.request(forURL: url!)

    request.HTTPMethod = httpMethod
    self.customOAuth2Manager.setupRequest(request)

    for (key, value) in headers {
        request.setValue(value, forHTTPHeaderField:key)
    }

    if let body = body where body != "" {
        let postData = (body as NSString).dataUsingEncoding(NSUTF8StringEncoding) …
Run Code Online (Sandbox Code Playgroud)

caching nsurlcache nsurlsession nsurlsessionconfiguration swift

20
推荐指数
2
解决办法
1万
查看次数

解析触发器时出错:找不到模块“csv-parse/sync”

我正在使用 Firebase 函数构建一个解析 CSV 文件的 API。

当我尝试使用 csv-parse/sync 而不是 csv-parse 时,部署到 Firebase Functions 失败并出现以下错误:

Error: Error parsing triggers: Cannot find module 'csv-parse/sync''
Require stack:
- /Users/xxx/Programming/xxx/Firebase Functions/xxx/functions/lib/index.js
- /usr/local/lib/node_modules/firebase-tools/lib/deploy/functions/runtimes/node/triggerParser.js

Try running "npm install" in your functions directory before deploying.
Run Code Online (Sandbox Code Playgroud)

我已经使用以下方式导入:

import { parse } from 'csv-parse/sync';

然后在这样的代码中使用:

interface EventData {
    update: string;
    id: string;
    title: string;
    description: string;
    category: string;
    ages: string;
    place: string;
    placeCoordinate: string;
    startDate: string;
    startTime: string;
    length: string;
    url: string;
    arrName: string;
  }

let events: Array<EventData> …
Run Code Online (Sandbox Code Playgroud)

node.js typescript google-cloud-functions firebase-cli

18
推荐指数
1
解决办法
1万
查看次数

在Unity3d中获取App Bundle版本

简单的问题,但似乎很难找到.

我正在构建Android和iOS游戏.我想提取应用程序的版本(即"2.0.1")(如果App Store/Google Play上有更新的版本,则显示弹出窗口).

有人知道如何以编程方式执行此操作吗?

c# unity-game-engine

15
推荐指数
3
解决办法
3万
查看次数

未找到"Google/Analytics.h"文件 - XCode 7

我在将Google AnalyticsSDK集成到iOS项目时遇到了很多麻烦.我正在使用XCode 7并以iOS 7为目标.使用Swift 2.0.但是我可以让样本正常工作(尽管不转换为Swift 2.0).

我已尝试通过CocoaPods安装并手动复制文件:

https://developers.google.com/analytics/devguides/collection/ios/v3/sdk-download

通过CocoaPods安装时,我尝试了两种方法

pod 'Google/Analytics'
Run Code Online (Sandbox Code Playgroud)

,

pod 'GoogleAnalytics'
Run Code Online (Sandbox Code Playgroud)

pod 'Google/Analytics', '~> 1.0.0'
Run Code Online (Sandbox Code Playgroud)

无论哪种情况,XCode构建都会失败并出现错误

BridgingHeader.h:2:9: 'Google/Analytics.h' file not found

Failed to import bridging header '/Users/jonas.andersson/Projects/MyAppName/MyAppName/Supporting files/BridgingHeader.h'
Run Code Online (Sandbox Code Playgroud)

这个在行:

#import <Google/Analytics.h>
Run Code Online (Sandbox Code Playgroud)

我也尝试过添加

$(SRCROOT)/Pods/GoogleAnalytics
Run Code Online (Sandbox Code Playgroud)

添加到AppDelegate时找不到Google/Analytics.h文件中的其他建议

更新

使用pod 'GoogleAnalytics'然后#import <Google/Analytics.h>更好地工作.但是,我收到以下错误:

Use of unresolved identifier 'GGLContext'
Run Code Online (Sandbox Code Playgroud)

当我根据Google文档尝试设置GA时:

var configureError:NSError?
GGLContext.sharedInstance().configureWithError(&configureError)
Run Code Online (Sandbox Code Playgroud)

xcode cocoapods google-analytics-sdk ios9 xcode7

14
推荐指数
2
解决办法
7115
查看次数

MonoDevelop 3.1.1找不到System.Xml.Linq

我刚刚下载了适用于Mac OS X的"Mono for Android"/ MonoDevelop 3.1.1的试用版.我正在尝试使用XDocument解析一些XML.但我似乎无法找到我需要的导入:

using System.Xml.Linq;
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

The type or namespace name 'Linq' does not exist in the namespace 'System.xml'. Are you missing an assembly reference?
Run Code Online (Sandbox Code Playgroud)

在解决方案资源管理器中的引用下打开System.Xml引用显示其中没有Linq:

System.Xml
 - References
   - mscorlib
   - System
 - System.Xml
 - System.Xml.Schema
 - System.Xml.Serialization
 - System.Xml.XPath
 - System.Xml.Xsl
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

编辑:更多信息:

  • 运行时:单声道2.10.9(tarball)

  • 适用于Android的单声道:4.4.55(评估)

  • Monotouch:Xamarin.Mac:未安装

  • 操作系统:Mac OS X 10.8.2

的System.Xml:

  • [assembly:AssemblyVersion("2.0.5.0")]

c# linq mono monodevelop xamarin.mobile

13
推荐指数
1
解决办法
9224
查看次数

'*** - [__ NSArrayM insertObject:atIndex:]:在执行UITableView动画重新加载时,对象不能为nil'

问题总结:当我在有很多细胞经常死机UITableView动画的高度时UITableViewCellUITextView编辑它的文本.使用iOS 8自定义单元格.

长问题:我已经成功实现了所以我可以动态地使用iOS 8自定义单元格将文本输入到单元格UITextView中并更改单元格高度而不会失去焦点(firstReponder).但是,如果tableView太大(行太多),它会崩溃.这是我的堆栈跟踪:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
*** First throw call stack:
(
0   CoreFoundation                      0x000000010b3b3d85 __exceptionPreprocess + 165
1   libobjc.A.dylib                     0x000000010b9cbdeb objc_exception_throw + 48
2   CoreFoundation                      0x000000010b274cc5 -[__NSArrayM insertObject:atIndex:] + 901
3   UIKit                               0x0000000108b05439 __46-[UITableView _updateWithItems:updateSupport:]_block_invoke1029 + 180
4   UIKit                               0x0000000108a7e838 +[UIView(UIViewAnimationWithBlocks) _setupAnimationWithDuration:delay:view:options:factory:animations:start:animationStateGenerator:completion:] + 582
5   UIKit                               0x0000000108a7ec6d +[UIView(UIViewAnimationWithBlocks) animateWithDuration:delay:options:animations:completion:] + 105
6   UIKit                               0x0000000108b05048 -[UITableView _updateWithItems:updateSupport:] + 4590 …
Run Code Online (Sandbox Code Playgroud)

uitableview ios

13
推荐指数
1
解决办法
2569
查看次数

设置SearchView清除按钮颜色

我正在使用工具栏创建一个searchview onCreateOptionsMenu,但无法获得X最初为白色的清除按钮.开始输入字母时变为白色.清理后它也会保持白色.

清除按钮的示例具有灰色而不是白色

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.responsible_menu, menu);
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); //TODO: May not be needed?

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
    {
        @Override
        public boolean onQueryTextSubmit(String query)
        {
            mAdapter.updateUIWithFilter(query);
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText)
        {
            mAdapter.updateUIWithFilter(newText);
            return false;
        }
    });

    // Does not work! Still not white.
    ImageView searchClose = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
    searchClose.setColorFilter(Color.argb(255, 255, 255, 255));

    searchClose.setAlpha(255);

    return true;
}
Run Code Online (Sandbox Code Playgroud)

responsible_menu.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android" …
Run Code Online (Sandbox Code Playgroud)

android searchview android-support-library

11
推荐指数
3
解决办法
9493
查看次数

从iOS服务检测屏幕开/关

我正在开发一个在后台作为服务运行的网络监视器应用程序.屏幕打开或关闭时是否可以收到通知/电话?

它通过使用以下代码存在于Android中:

private void registerScreenOnOffReceiver()
{
   IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
   filter.addAction(Intent.ACTION_SCREEN_OFF);
   registerReceiver(screenOnOffReceiver, filter);
}
Run Code Online (Sandbox Code Playgroud)

然后在打开/关闭屏幕时调用screenOnOffReceiver.iOS有类似的解决方案吗?

编辑: 到目前为止我发现的最好的是UIApplicationProtectedDataWillBecomeUnavailable(检测iPhone屏幕是否打开/关闭),但它要求用户在设备上启用数据保护(密码保护).

notifications objective-c broadcast jailbreak ios

10
推荐指数
1
解决办法
1万
查看次数

使用基于协议或基类的泛型参数值将子类上的方法调用到UITableViewCell

我想简化在UITableViewCell子类中调用"setup"方法的过程.但是,并非所有设置方法都相同,但它们的参数继承自相同类型.是否可以使用泛型或协议不必每次都抛出参数?

首先,我是一个像这样的cellForRow方法:

class DataSource<V : UIViewController, T: TableViewCellData, VM: ViewModel> : NSObject, UITableViewDataSource, UITableViewDelegate {

    var dataCollection: TableViewDataCollection<T>!
    var viewModel: VM!

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellData = dataCollection.object(for: indexPath)
        let cell = tableView.dequeueReusableCell(withIdentifier: cellData.identifier(), for: indexPath)
        if let setupableCell = cell as? CellDataSetupable {
            setupableCell.setup(with: cellData, viewModel: viewModel)
        }
        return cell
    }
}

protocol CellDataSetupable : class {
    func setup(with cellData: TableViewCellData, viewModel: ViewModel)
}
Run Code Online (Sandbox Code Playgroud)

我使用cellData和viewModel设置单元格.

在我的(很多)自定义UITableViewCell子类中:

extension BlurbTableViewCell : CellDataSetupable {
    func …
Run Code Online (Sandbox Code Playgroud)

generics ios swift

9
推荐指数
1
解决办法
1003
查看次数