小编ran*_*dom的帖子

静态全局变量的奇怪行为

我知道这个程序没有以适当的方式使用静态变量,但它显示了如何重现我看到的行为:

Main.cpp:

int main(){
    MyObject* p = new MyObject();
    Header::i = 5;

    printf("i %i\n", Header::i);
    p->update();

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

MyObject.cpp:

MyObject::MyObject(){
}

void MyObject::update(){
    printf("i %i\n", Header::i);
}
Run Code Online (Sandbox Code Playgroud)

Extern.h:

namespace Header {
    static int i;
};
Run Code Online (Sandbox Code Playgroud)

我得到的输出是:

i : 5
i : 0
Run Code Online (Sandbox Code Playgroud)

为什么我没有得到5两个输出?这0是从哪里来的?你能解释静态变量是如何工作的吗?

c++ static global

28
推荐指数
6
解决办法
2495
查看次数

具有不同单元格宽度的RecyclerView Grid的LayoutManager

StaggeredGridLayoutManager似乎不允许自定义单元格宽度或跨越多个列(垂直方向除外)以进行垂直方向.

在此输入图像描述

LayoutManager如上所示组织细胞的优选方法是什么?

PS我只是想知道如何自定义单元格宽度而不是高度StaggeredGridLayoutManager.我知道高度可以按照此示例中的实现进行定制.

public class VerticalStaggeredGridFragment extends RecyclerFragment {

    public static VerticalStaggeredGridFragment newInstance() {
        VerticalStaggeredGridFragment fragment = new VerticalStaggeredGridFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected RecyclerView.LayoutManager getLayoutManager() {
        return new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
    }

    @Override
    protected RecyclerView.ItemDecoration getItemDecoration() {
        return new InsetDecoration(getActivity());
    }

    @Override
    protected int getDefaultItemCount() {
        return 100;
    }

    @Override
    protected SimpleAdapter getAdapter() {
        return new SimpleStaggeredAdapter();
    }
}
Run Code Online (Sandbox Code Playgroud)

适配器

public class SimpleStaggeredAdapter …
Run Code Online (Sandbox Code Playgroud)

android gridview android-recyclerview

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

图像变形 - 凸起效应算法

可以指出图像变形算法吗?专门用于凸起效果?

algorithm image image-processing

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

在rails&carrierwave上传之前显示图像预览的最佳方式

我过去几天一直在使用rails,想知道在上传rails&carrierwave之前显示图像预览的最佳方式是什么.

我遇到了一些选项,比如使用plupload或jquery文件上传或使用uploadify.

upload ruby-on-rails image preview carrierwave

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

使用rails中的link_to提交表单

我正在尝试使用link_to提交表单,如下所示:

 <%= form_for(@post,  :url=> '/post/action', :method=> 'post', :html => {:id=>'form_id'} ) do |f| %>
  ....

 <%= link_to 'submit', "/post/action", :onclick=>"document.getElementById('form_id').submit()" %>

  ....
Run Code Online (Sandbox Code Playgroud)

但它没有发布表单,它只是将我的表单重定向到指定的URL.有谁知道如何做到这一点?

ruby-on-rails link-to

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

使用Rails服务器推送通知

我有一个rails服务器和一个后端.是否可以使用我的rails服务器向iPhone和Android应用程序发送推送消息?

iphone android ruby-on-rails push-notification

11
推荐指数
2
解决办法
5568
查看次数

服务器为每个客户端发送事件,Puma,Rails和最大专用线程

我正在使用Redis for我的rails项目订阅频道并在事件发生时发布到这些频道.在客户端,我正在注册与这些频道对应的EventSource.每当服务器上的订阅频道发生事件时,服务器执行SSE写入,以便所有已注册的客户端都接收更新.

现在,与订阅这些通道的每个客户端的服务器连接保持活动状态,即专用于此客户端的服务器线程一直运行,直到客户端断开连接.使用这种方法,如果有1000个并发用户订阅了一个频道,我将打开1000个TCP/IP连接.

我正在使用Puma作为本教程中建议的Web服务器.Puma默认指定16个最大线程.我可以将此限制更改为更高的限制.

我可能不知道我的应用程序中一次可能有多少并发用户,并且不知道最大值是多少.我可以在Puma中指定的线程数.在最坏的情况下,如果专用于每个并发用户的线程数达到为Puma网络服务器指定的线程的最大计数,则应用程序将冻结所有用户,直到其中一个并发用户断开连接.

我很高兴使用Rails实时流,并且服务器在我的rails项目中发送了事件,但是使用这种方法我有可能达到我的Web服务器中指定的最大线程的限制,因此应用程序对所有用户都没有响应,直到其中一个并发用户断开连接.

不确定Puma对于大型并发用户群的典型最大线程数是多少.

我应该考虑其他方法 - 可能是基于ajax的轮询或使用事件驱动的非阻塞I/O模型的Node.js?或者只是运行一些基准来了解我的最大线程数是多少?

concurrency multithreading ruby-on-rails server-sent-events puma

11
推荐指数
1
解决办法
1558
查看次数

textFieldShouldBeginEditing + UIKeyboardWillShowNotification + OS 3.2

我在UIView上有多个文本字段.

我在textFieldShouldBeginEditing方法中退出前一个textField,执行以下事件序列

  • 收到对应于隐藏前一个字段的键盘的字段的UIKeyboardWillHideNotification.

  • 方法textFieldShouldBeginEditing返回YES然后

  • 收到UIKeyboardWillShowNotification,显示当前字段的键盘.

但是,在OS 3.2中,即使textFieldShouldBeginEditing返回YES,也不会收到当前字段的UIKeyboardWillShowNotification.

该逻辑适用于OS <3.2

我可能做错了什么想法?

列在我的代码的一部分下面(xib中只有两个文本字段).

我需要在keyboardWillShow和keyboardWillHide上执行一组操作查看在OS 3.2和OS <3.2中运行代码的区别

任何人都可以解释行为上的差异吗?

.H

@interface ExampleViewController : UIViewController  
{
    IBOutlet UITextField *numericTextField;
    IBOutlet UITextField *alphaTextField;   
    UITextField *lastTextField;
    int lastCursorPos;
    int cursorPosition;
    NSMutableArray *textFields;
}

@property (nonatomic, retain) UITextField *lastTextField;
@property (nonatomic, retain) NSMutableArray *textFields;

@end
Run Code Online (Sandbox Code Playgroud)

.M

- (void)viewWillAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) 
                                                 name:UIKeyboardWillShowNotification object:self.view.window]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) 
                                                 name:UIKeyboardWillHideNotification object:self.view.window]; 

    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.textFields = [[NSMutableArray alloc] initWithCapacity:2];
    [self.textFields insertObject:alphaTextField atIndex:0];
    [self.textFields insertObject:numericTextField atIndex:1];
    cursorPosition = …
Run Code Online (Sandbox Code Playgroud)

iphone sdk uikeyboard

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

Core-Plot PieChart切片颜色

我在我的一个iPhone项目中使用核心情节.是否可以更改饼图中所选切片的颜色(使用CPPieChartDataSource,CPPieChartDelegate)?

iphone core-plot

10
推荐指数
2
解决办法
5471
查看次数

推送Android和iPhone的通知

我正在开发一个Android和iPhone应用程序,并希望向平台用户发送推送通知 - 即使在设备关闭时,也可以使用任何主要新闻进行简单弹出.

我应该采取哪些方式?

iphone android push-notification

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